分类:HIHOCODER

HIHOCODER-WEEK135 九宫

HIHOCODER-WEEK135 九宫

DFS
其实一共就八种填法……
而且似乎没优化的裸DFS也没问题……干脆就偷懒没写优化= =
======================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HIHOWEED135
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] S = new int[9];
            string[] inp = Console.ReadLine().Trim().Split(' ');
            S[0] = Convert.ToInt32(inp[0]);
            S[1] = Convert.ToInt32(inp[1]);
            S[2] = Convert.ToInt32(inp[2]);
            inp = Console.ReadLine().Trim().Split(' ');
            S[3] = Convert.ToInt32(inp[0]);
            S[4] = Convert.ToInt32(inp[1]);
            S[5] = Convert.ToInt32(inp[2]);
            inp = Console.ReadLine().Trim().Split(' ');
            S[6] = Convert.ToInt32(inp[0]);
            S[7] = Convert.ToInt32(inp[1]);
            S[8] = Convert.ToInt32(inp[2]);
            bool[] C = new bool[9] { true, true, true, true, true, true, true, true, true };
            foreach (var i in S)
            {
                if (i == 0) continue;
                C[i - 1] = false;
            }
            DFS(S, C, 0);
            if (ans == 1)
            {
                for (int i = 0; i < 9; i++)
                {
                    Console.Write(AnsS[i]);
                    if (((i + 1) % 3 == 0))
                        Console.WriteLine();
                    else Console.Write(' ');
                }
            }
            else
                Console.WriteLine("Too Many");
        }
        static int ans = 0;
        static int[] AnsS = new int[9];
        static void DFS(int[] S, bool[] Ava, int Level)
        {
            if (Level > 8)
            {
                if (Check(S))
                {
                    ans++;
                    S.CopyTo(AnsS, 0);
                }
                return;
            }
            if (S[Level] != 0)
            {
                DFS(S, Ava, Level + 1);
                return;
            }
            for (int i = 0; i < 9; i++)
            {
                if (Ava[i] == false) continue;
                Ava[i] = false;
                S[Level] = i + 1;
                DFS(S, Ava, Level + 1);
                Ava[i] = true;
                S[Level] = 0;
            }
            return;
        }
        static bool Check(int[] S)
        {
            if ((S[0] + S[1] + S[2]) == 15)
                if ((S[3] + S[4] + S[5]) == 15)
                    if ((S[6] + S[7] + S[8]) == 15)
                        if ((S[0] + S[3] + S[6]) == 15)
                            if ((S[1] + S[4] + S[7]) == 15)
                                if ((S[2] + S[5] + S[8]) == 15)
                                    if ((S[0] + S[4] + S[8]) == 15)
                                        if ((S[2] + S[4] + S[6]) == 15)
                                            return true;
            return false;
        }
    }
}

[C#]二叉树前序遍历中序遍历求后序遍历

[C#]二叉树前序遍历中序遍历求后序遍历

基础算法题,主要是递归
http://hihocoder.com/problemset/problem/1049

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HIHOCODER_WEEK010
{
    class Program
    {
        static void Main(string[] args)
        {
            string fstOrder = Console.ReadLine();
            string midOrder = Console.ReadLine();
            Node root = solve(fstOrder, midOrder);
            lstOrder(root);
        }
        static Node solve(string fstOrder,string midOrder)
        {
            if (fstOrder.Length == 1 && midOrder.Length == 1)
            {
                Node newNode = new Node(fstOrder[0]);
                return newNode;
            }
            char Mid = fstOrder[0];
            string newMidOrderL = midOrder.Split(Mid)[0];
            string newMidOrderR = midOrder.Split(Mid)[1];
            string newFstOrderL = fstOrder.Substring(1, newMidOrderL.Length);
            string newFstOrderR = fstOrder.Substring(newMidOrderL.Length + 1, newMidOrderR.Length);
            Node node = new Node(Mid);
            Node nodeL = new Node(), nodeR = new Node();
            if (newFstOrderL.Length!=0)
            {
                nodeL = solve(newFstOrderL, newMidOrderL);
            }
            if (newFstOrderR.Length!=0)
            {
                nodeR = solve(newFstOrderR, newMidOrderR);
            }
            node.L = nodeL;
            node.R = nodeR;
            return node;
        }
        static void lstOrder(Node root)
        {
            if (root.L != null) lstOrder(root.L);
            if (root.R != null) lstOrder(root.R);
            if (root.value != '\0') Console.Write(root.value);
        }
    }
    public class Node
    {
        public char value;
        public Node L;
        public Node R;
        public Node()
        {
            value = '\0';
            L = null;
            R = null;
        }
        public Node(char c)
        {
            value = c;
            L = null;
            R = null;
        }
    }
}
HIHOCODER-WEEK133 2-SAT·hihoCoder音乐节

HIHOCODER-WEEK133 2-SAT·hihoCoder音乐节

http://hihocoder.com/contest/hiho133/problem/1
问题的思路就是转化为图论问题。
2-SAT问题:https://en.wikipedia.org/wiki/2-satisfiability
双重满足性。也就是对于A or B这样的形式,我们连接两条边¬a->b和¬b->a,然后对于求出来的图求强连通。
不过这个题数据挺小的我就对应的点DFS,比如三首歌,我就1,4对应DFS若互相存在在对方的DFS序列中则BAD。若不存在相互存在的点(也就是非强连通)则GOOD。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HIHOCODER_WEEK_133
{
    class Program
    {
        static List<int> L;
        static List<int> V;
        static void Main(string[] args)
        {
            int K = Convert.ToInt32(Console.ReadLine());
            for (int loop = 0; loop < K; loop++)
            {
                string[] inp;

                inp = Console.ReadLine().Split(' ');
                int n = Convert.ToInt32(inp[0]), m = Convert.ToInt32(inp[1]);
                byte[,] G = new byte[2 * n + 5, 2 * n + 5];
                for (int i = 0; i < m; i++)
                {
                    inp = Console.ReadLine().Split(' ');
                    bool F1 = (inp[0][0] == 'm' ? true : false);
                    bool F2 = (inp[1][0] == 'm' ? true : false);
                    int N1 = Convert.ToInt32(inp[0].Replace("m", "").Replace("h", ""));
                    int N2 = Convert.ToInt32(inp[1].Replace("m", "").Replace("h", ""));
                    int P1A = (F1 ? 1 : 0) * n + N1, P1B = (F2 ? 0 : 1) * n + N2;
                    int P2A = (F2 ? 1 : 0) * n + N2, P2B = (F1 ? 0 : 1) * n + N1;
                    G[P1A, P1B] = 1;
                    G[P2A, P2B] = 1;
                }
                bool ans = false;
                for (int i = 1; i <= n; i++)
                {
                    L = new List<int>();
                    DFS(G, 2 * n, i);
                    bool Fl1 = false;
                    if (L.Contains(i + n)) Fl1 = true;
                    L = new List<int>();
                    DFS(G, 2 * n, i + n);
                    bool Fl2 = false;
                    if (L.Contains(i)) Fl2 = true;
                    if (Fl1&&Fl2)
                    {
                        ans = true;
                        break;
                    } 
                }
                if (ans) Console.WriteLine("BAD");
                else Console.WriteLine("GOOD");
                
            }
        }
        static void DFS(byte[,] G,int n,int curr)
        {
            if (L.Contains(curr)) return;
            L.Add(curr);
            for (int i=1;i<=n;i++)
            {
                if (G[curr, i] == 1)
                {
                    G[curr, i] = 0;
                    DFS(G, n, i);
                    G[curr, i] = 1;
                }
            }
            return;
        }
    }
}
REPORT HIHOCODER1458-Parentheses Matching

REPORT HIHOCODER1458-Parentheses Matching

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Parentheses_Matching
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            Matcher m = new Matcher(s);

            Console.Read();
        }
    }
    
    public class Matcher
    {
        public Matcher(string s)
        {
            int cur = 0;
            Stack<char> A = new Stack<char>();
            Stack<int> cnt = new Stack<int>();
            SortedDictionary<int, int> d = new SortedDictionary<int, int>();
            foreach (var c in s)
            {
                cur++;
                if (c=='(')
                {
                    A.Push(c);
                    cnt.Push(cur);
                }
                if (c==')')
                {
                    A.Pop();
                    int a = cnt.Pop();
                    int b = cur;
                    d.Add(a, b);
                }
            }
            foreach (var k in d)
            {
                Console.WriteLine("{0} {1}", k.Key, k.Value);
            }
        }
    }
}

HIHOCODER-WEEK127 后缀自动机一·基本概念

HIHOCODER-WEEK127 后缀自动机一·基本概念

讲道理我还没弄懂= =不过先把题目做出来了
并不知道怎么在求SAM的时候求出ENDPOS只好求完SAM之后再枚举子串在SAM中匹配到state然后再输出结果
我知道我蠢qwq
=================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HIHOCODER_WEEK127
{
    class Program
    {
        const int MAXLEN = 100;
        static string S;
        static void Main(string[] args)
        {
            S = Console.ReadLine();
            State[] st = new State[MAXLEN * 2];
            for (int i = 0; i < MAXLEN * 2; i++)
            {
                st[i] = new State();
            }
            int sz = 0, last = 0;
            st[0].len = 0;
            st[0].link = -1;
            ++sz;
            int charpos = 0;
            foreach (char c in S)
            {
                charpos++;
                int cur = sz++;
                st[cur].len = st[last].len + 1;
                int p;
                for (p = last; p != -1 && (!st[p].next.ContainsKey(c)); p = st[p].link)
                {
                    st[p].next.Add(c, cur);
                }
                if (p == -1)
                {
                    st[cur].link = 0;
                }
                else
                {
                    int q = st[p].next;
                    if (st[p].len + 1 == st[q].len)
                    {
                        st[cur].link = q;
                    }
                    else
                    {
                        int backup = sz++;
                        st[backup].len = st[p].len + 1;
                        st[backup].next = new Dictionary<char, int>(st[q].next);
                        st[backup].link = st[q].link;
                        for (; p != -1 && (st[p].next == q); p = st[p].link)
                        {
                            st[p].next = backup;
                        }
                        st[q].link = st[cur].link = backup;
                    }
                }
                last = cur;
            }
            for (int i = 0; i < S.Length; i++)
            {
                st[0].endpos.Add(i);
            }
            for (int i = 0; i < S.Length; i++)
            {
                for (int j = i + 1; j < S.Length + 1; j++)
                {
                    string substr = S.Substring(i, j - i);
                    int state = Match(substr, st);
                    if (state != -1)
                    {
                        st[state].endpos.Add(j);
                        st[state].sub.Add(substr);
                    }
                }
            }
            int n = Convert.ToInt32(Console.ReadLine().Trim());
            for (int i = 0; i < n; i++)
            {
                string q = Console.ReadLine();
                foreach (var t in st)
                {
                    if (t.sub.Contains(q))
                    {
                        int max = 0;
                        int min = 0x3f3f3f3f;
                        string maxstring = "";
                        string minstring = "";
                        foreach (var ts in t.sub)
                        {
                            if (ts.Length > max) { max = ts.Length; maxstring = ts; }
                            if (ts.Length < min) { min = ts.Length; minstring = ts; }
                        }
                        Console.Write("{0} {1} ", minstring, maxstring);
                        foreach (var v in t.endpos)
                        {
                            Console.Write("{0} ", v);
                        }
                        Console.WriteLine();
                    }
                }
            }

        }

        static int Match(string s,State[] st)
        {
            bool m = true;
            int l = s.Length;
            if (!st[0].next.ContainsKey(s[0])) return -1;
            int cur = 0;
            while (l-- > 0)
            {
                char c = s[s.Length - l - 1];
                if (!st[cur].next.ContainsKey(c))
                {
                    m = false;
                    break;
                }
                cur = st[cur].next;
            }
            return m ? cur : -1;
        }
    }
    public class State
    {
        public int len, link;
        public HashSet<string> sub;
        public HashSet<int> endpos;
        public Dictionary<char, int> next = new Dictionary<char, int>();
        public State()
        {
            len = 0;
            link = 0;
            sub = new HashSet<string>();
            endpos = new HashSet<int>();
            next = new Dictionary<char, int>();
        }
    }
}

REPORT HIHOCODER1354-积水的城市

REPORT HIHOCODER1354-积水的城市

SPFA
本来想是做一个图然后把积水点相连的路径删掉然后发现这样做好蠢
这个程序的C#代码我写得很难看……贴http://blog.csdn.net/piaocoder/article/details/52173939的代码吧

#include <iostream>
#include <set> 
#include <queue>
#include <cstring>
  
#define INF 0x3f3f3f3f  
using namespace std;  
  
const int N = 505;  
const int M = 505;  
const int dx[] = {-1,0,1,0};  
const int dy[] = {0,1,0,-1};  
  
struct node{  
    int x,y,d;  
    node(int a,int b,int c) : x(a),y(b),d(c){}  
    bool operator < (const node &s) const{  
        return d > s.d;  
    }  
};  
  
int n,m;  
int A[M];  
int B[N];  
int vis[N][M];  
set<pair<int, int> > dead;  
  
int solve(int x,int y,int p,int q){  
    priority_queue<node> pq;  
    pq.push(node(x,y,0));  
    vis[x][y] = 0;  
    while(!pq.empty()){  
        node cur = pq.top();  
        pq.pop();   
        for(int i = 0; i < 4; ++i) {  
            int xx = cur.x + dx[i];  
            int yy = cur.y + dy[i];  
            if(xx <= 0 || yy <= 0 || xx > n || yy > m || dead.find(make_pair(xx, yy)) != dead.end())  
                continue;  
            int d = dx[i] == 0 ? cur.d + A[min(cur.y, yy)]:cur.d + B[min(cur.x, xx)];  
            if (vis[xx][yy] <= d)  
                continue;  
            if (xx != p || yy != q)  
                pq.push(node(xx, yy, d));  
            vis[xx][yy] = d;  
        }//
    }  
    return vis[p][q] == INF?-1:vis[p][q];  
}  
  
int main(){  
    while(~scanf("%d%d",&n,&m)){  
        for(int i = 1; i < n; ++i)  
            scanf("%d",&B[i]);  
        for(int i = 1; i < m; ++i)  
            scanf("%d",&A[i]);  
        int k;  
        scanf("%d",&k);  
        for(int i = 0; i < k; ++i){  
            int x, y;  
            scanf("%d%d",&x,&y);  
            dead.insert(make_pair(x, y));  
        }  
        int Q;  
        scanf("%d",&Q);  
        for(int i = 0; i < Q; ++i){  
            int x,y,p,q;  
            scanf("%d%d%d%d",&x,&y,&p,&q);  
            memset(vis,INF,sizeof(vis));  
            printf("%d\n",solve(x,y,p,q));  
        }  
    }  
    return 0;  
}  
REPORT HIHOCODER1353-满减优惠

REPORT HIHOCODER1353-满减优惠

http://hihocoder.com/problemset/problem/1353
==========================================================================
枚举。
DP应该也是可以的
==========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HIHOCODER1353
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] inp;
            inp = Console.ReadLine().Trim().Split(' ');
            int n = Convert.ToInt32(inp[0]);
            int m = Convert.ToInt32(inp[1]);
            inp = Console.ReadLine().Trim().Split(' ');
            int[] price = new int[n];
            int totalvalue = 0;
            for (int i = 0; i < n; i++)
            {
                price[i] = Convert.ToInt32(inp[i]);
                totalvalue += price[i];
            }
            if (totalvalue < m)
            {
                Console.WriteLine(-1);
                return;
            }
            FIND f = new HIHOCODER1353.FIND(price, n, m);
            Console.WriteLine(f.ANS);
        }
    }
    class FIND
    {
        int[] value;
        int N, M;
        int CUR = 0;
        int MAX, SUM;
        public int ANS;
        public FIND(int[] v, int n, int m)
        {
            value = new int[n];
            v.CopyTo(value, 0);
            N = n;
            M = m;
            MAX = int.MaxValue;
            SUM = value.Sum();

            int max = 1 << N;
            for (int i = 1; i <= max; i++)
            {
                CUR = 0;
                for (int j = 0; j < N; j++)
                {
                    if (((1 << j) & i) != 0)
                    {
                        CUR += value[j];
                    }
                    if (CUR > SUM)
                    {
                        break;
                    }
                    if (CUR >= M && CUR <= MAX)
                    {
                        MAX = CUR;
                        break;
                    }
                }
            }
            ANS = MAX;
        }
    }
}

HIHOCODER-WEEK125 GeoHash编解码

HIHOCODER-WEEK125 GeoHash编解码

WEEK124我选择鸽了……

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeoHash
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] inp = Console.ReadLine().Trim().Split(' ');
            int N = Convert.ToInt32(inp[0]);
            int M = Convert.ToInt32(inp[1]);
            for (int i = 0; i < N; ++i)
            {
                inp = Console.ReadLine().Trim().Split(' ');
                decimal A = Convert.ToDecimal(inp[0]);
                decimal B = Convert.ToDecimal(inp[1]);
                GeoHash geohash = new GeoHash();
                Console.WriteLine(geohash.Encode(A, B, 10));
            }
            for (int i = 0; i < M; ++i)
            {
                string inpt = Console.ReadLine().Trim();
                GeoHash geohash = new GeoHash();
                decimal[] red = geohash.DeCode(inpt);
                Console.WriteLine("{0} {1}", red[0].ToString("f6"), red[1].ToString("f6"));
            }
        }
    }
    class GeoHash
    {
        private List<char> Base32Coding;
        public GeoHash()
        {
            Base32Coding = new List<char> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        }
        public string Encode(decimal latitude, decimal longitude, int precision)
        {
            decimal latitudeLeft = -90, latitudeRight = 90;
            decimal longitudeLeft = -180, longitudeRight = 180;
            int length = precision * 5;
            string hash = "";
            int bits = 0;
            for (int i = 1; i <= length; ++i)
            {
                if (i % 2 == 0)
                {
                    decimal mid = (latitudeLeft + latitudeRight) / 2;
                    if (latitude > mid)
                    {
                        bits = bits * 2 + 1;
                        latitudeLeft = mid;
                    }
                    else
                    {
                        bits = bits * 2;
                        latitudeRight = mid;
                    }
                }
                else
                {
                    decimal mid = (longitudeLeft + longitudeRight) / 2;
                    if (longitude > mid)
                    {
                        bits = bits * 2 + 1;
                        longitudeLeft = mid;
                    }
                    else
                    {
                        bits = bits * 2;
                        longitudeRight = mid;
                    }
                }
                if (i % 5 == 0)
                {
                    hash += Base32Coding[bits];
                    bits = 0;
                }
            }
            return hash;
        }
        public decimal[] DeCode(string hash)
        {
            bool odd = true;
            decimal latitudeLeft = -90, latitudeRight = 90;
            decimal longitudeLeft = -180, longitudeRight = 180;
            int length = hash.Length;
            for (int i = 0; i < length; ++i)
            {
                char currentcode = hash[i];
                int bits = Base32Coding.IndexOf(currentcode);
                for (int j = 4; j >= 0; --j)
                {
                    int bit = (bits >> j) & 1;
                    if (odd)
                    {
                        decimal mid = (longitudeLeft + longitudeRight) / 2m;
                        if (1 - bit == 0)
                            longitudeLeft = mid;
                        else longitudeRight = mid;
                    }
                    else
                    {
                        decimal mid = (latitudeLeft + latitudeRight) / 2m;
                        if (1 - bit == 0)
                            latitudeLeft = mid;
                        else latitudeRight = mid;
                    }
                    odd = !odd;
                }
            }
            decimal[] ret = new decimal[2];
            ret[0] = (latitudeLeft + latitudeRight) / 2;
            ret[1] = (longitudeLeft + longitudeRight) / 2;
            return ret;
        }
    }
}

REPORT-The 2016 ACM-ICPC Asia Beijing Regional Contest Problem D. What a Beautiful Lake

REPORT-The 2016 ACM-ICPC Asia Beijing Regional Contest Problem D. What a Beautiful Lake

Problem D. What a Beautiful Lake
Description
Weiming Lake, also named “Un-named Lake”, is the most famous scenic spot in Peking University. It is located in the north of the campus and is surrounded by walking paths, small gardens, and old red buildings with typical Chinese curly roofs. The lake was once the royal garden in Qing Dynasty. Boya tower stands on a little hill beside the lake. The lake and the tower form a distinctive landscape and a symbol of Peking University.
Weiming Lake is a very good place for studying, reading, skating in the winter, and of course, jogging. More and more students and teachers run or walk around Weiming Lake every day and show how many paces they have covered in the mobile app WeChat Sports to get “Zans” (applauses).
ACMer X also enjoys jogging around Weiming Lake. His watch can measure and record an altitude value every meter. After a round of running, X collected the altitude data around the lake. Now he wants to find out the longest slope around the lake.
Input
There are no more than 20 test cases.
Each case has two lines.
The first line is an integer N (2 <= N <= 100) meaning that the length of the road around the lake is N meters. The second line contains N integers a1,a2...aN, (0<= a1,a2...aN <= 100) indicating N altitude sample values around the lake. The samples are given in clockwise order, and the distance between two adjacent samples is one meter. Of course the distance between a1 and aN is also one meter. The input ends by a line of 0. Output For each test case, print the length of the longest slope in meters. A slope is a part of the road around the lake, and it must keep going up or going down. If there are no slope, print 0. Sample Input 4 1 1 1 1 8 5 1 2 3 4 5 6 2 6 5 4 3 2 1 2 10 1 0 2 3 2 2 3 4 3 2 0 Sample Output 0 5 4 4 ============================================================== 最长连续上升子序列 将数组复制一遍连接起来,然后正着反着做两遍LICS就好了 ============================================================== [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace What_a_Beautiful_Lake { class Program { static void Main(string[] args) { int n = 0; do { n = Convert.ToInt32(Console.ReadLine().Trim()); if (n == 0) break; int[] a = new int[n * 2]; string[] inp = Console.ReadLine().Trim().Split(' '); for (int i = 0; i < n; i++) { a[i] = Convert.ToInt32(inp[i]); a[i + n] = Convert.ToInt32(inp[i]); } int len = LICS(a); Array.Reverse(a); len = Math.Max(LICS(a), len); Console.WriteLine(len - 1); } while (true); } static int LICS(int[] A) { if (A.Length == 0) { return 0; } else if (A.Length == 1) { return 1; } int sublen = 0, templen = 1; for (int i = 1; i < A.Length; i++) { if (A[i] > A[i - 1]) { templen++; } else { templen = 1; } if (sublen < templen) { sublen = templen; } } return sublen; } } } [/csharp]

REPORT-hihoCoder太阁最新面经算法竞赛15(AC #1 #2 #3) – 16.11.19

REPORT-hihoCoder太阁最新面经算法竞赛15(AC #1 #2 #3) – 16.11.19

#1 Boarding Passes
如果有一个城市A,不在终点城市里或者它作为终点城市的次数比比作为起点城市的次数少一次,那么这个就是总起点城市。
总终点城市同理。
最开始我还以为是拓扑排序,结果只过了20%然后才意识到这个图可能会有环。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Boarding_Passes
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine().Trim());
            
            Dictionary<string, int> startcities = new Dictionary<string, int>();
            Dictionary<string, int> endcities = new Dictionary<string, int>();
            int startcitycount = 0, endcitycount = 0;
            for (int i = 0; i < n; ++i)
            {
                string[] inp = Console.ReadLine().Trim().Split();
                if (!startcities.ContainsKey(inp[0]))
                {
                    startcitycount++;
                    startcities.Add(inp[0], 1);
                }
                else
                {
                    startcities[inp[0]]++;
                }
                if (!endcities.ContainsKey(inp[1]))
                {
                    endcitycount++;
                    endcities.Add(inp[1], 1);
                }
                else
                {
                    endcities[inp[1]]++;
                }
            }

            foreach (var temp in startcities)
            {
                if (!endcities.ContainsKey(temp.Key) || endcities[temp.Key] + 1 == startcities[temp.Key])
                {
                    Console.Write(temp.Key);
                    break;
                }
            }
            Console.Write(" ");
            foreach (var temp in endcities)
            {
                if (!startcities.ContainsKey(temp.Key) || startcities[temp.Key] + 1 == endcities[temp.Key])
                {
                    Console.Write(temp.Key);
                    break;
                }
            }
        }
    }
}

#2 Sorting Photo Files
排序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sorting_Photo_Files
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> partStr = new List<string>();
            Dictionary<string, List<int>> partInt = new Dictionary<string, List<int>>();
            int N = Convert.ToInt32(Console.ReadLine().Trim());
            for (int i = 0; i < N; i++)
            {
                string filename = Console.ReadLine().Trim();
                var sp = split(filename);
                if (!partInt.ContainsKey(sp.Key))
                {
                    List<int> l = new List<int>();
                    partStr.Add(sp.Key);
                    l.Add(sp.Value);
                    partInt.Add(sp.Key, l);
                }
                else
                {
                    partInt[sp.Key].Add(sp.Value);
                }
            }
            foreach(var temp in partInt)
            {
                temp.Value.Sort();
            }
            partStr.Sort();
            foreach(var temp in partStr)
            {
                foreach (var temp2 in partInt[temp])
                {
                    Console.WriteLine("{0}{1}", temp, temp2);
                }
            }
        }
        public static KeyValuePair<string, int> split(string str)
        {
            KeyValuePair<string, int> retdata = new KeyValuePair<string, int>();
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= '0' && str[i] <= '9')
                {
                    string partA = str.Substring(0, i);
                    string partB = str.Substring(i);
                    retdata = new KeyValuePair<string, int>(partA, Convert.ToInt32(partB));
                    break;
                }
            }
            return retdata;
        }
    }
}

#3 Circle Detect
判环。DFS。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Circle_Detect
{
class Program
{
static void Main(string[] args)
{
int T = Convert.ToInt32(Console.ReadLine().Trim());
for (int Times = 0; Times < T; Times++) { string[] inp = Console.ReadLine().Trim().Split(' '); int n = Convert.ToInt32(inp[0]); int m = Convert.ToInt32(inp[1]); List[] g = new List[n + 5];
for (int i = 0; i < n + 5; i++) { g[i] = new List();
}
bool[] visited = new bool[n + 5];
for (int i = 0; i < n + 5; i++) visited[i] = false; for (int i = 0; i < m; i++) { inp = Console.ReadLine().Trim().Split(' '); int a = Convert.ToInt32(inp[0]) - 1; int b = Convert.ToInt32(inp[1]) - 1; if (!g[a].Contains(b)) g[a].Add(b); } bool flag = false; for (int i = 0; i < n; i++) { if (dfs(visited, i, g)) { flag = true; break; } } if (flag) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } static bool dfs(bool[] vis,int p,List[] g)
{
if (vis[p]) return true;
vis[p] = true;
for (int i = 0; i < g[p].Count(); i++) { if (dfs(vis, g[p][i], g)) return true; } vis[p] = false; return false; } } }