P1341-无序字母对

  1 #include <bits/stdc++.h>
  2 #define _for(i,a,b) for(int i = (a);i < b;i ++)
  3 #define _rep(i,a,b) for(int i = (a);i > b;i --)
  4 #define INF 0x3f3f3f3f
  5 #define pb push_back
  6 typedef long long ll;
  7 using namespace std;
  8 inline ll read()
  9 {
 10     ll ans = 0;
 11     char ch = getchar(), last = ' ';
 12     while(!isdigit(ch)) last = ch, ch = getchar();
 13     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
 14     if(last == '-') ans = -ans;
 15     return ans;
 16 }
 17 inline void write(ll x)
 18 {
 19     if(x < 0) x = -x, putchar('-');
 20     if(x >= 10) write(x / 10);
 21     putchar(x % 10 + '0');
 22 }
 23 
 24 int sc4(char c)
 25 {
 26     if(islower(c))
 27         return c-'a'+26;
 28     else
 29         return c-'A';
 30 }
 31 int sc3(int k)
 32 {
 33     if(k<=25)
 34         return 'A'+k;
 35     else
 36         return 'a'+k-26;
 37 }
 38 int N;
 39 vector<vector<int>> v(100);
 40 int vis[100][100];
 41 string ans;
 42 bool dfs(int nw)
 43 {
 44     if(ans.size()==N+1)
 45         return true;
 46 
 47     _for(i,0,v[nw].size())
 48     {
 49         if(vis[nw][v[nw][i]]>0)
 50         {
 51             vis[nw][v[nw][i]] --;
 52             vis[v[nw][i]][nw] --;
 53             ans += sc3(v[nw][i]);
 54             if(dfs(v[nw][i])) return true;
 55             vis[v[nw][i]][nw] ++;
 56             vis[nw][v[nw][i]] ++;
 57             ans.pop_back();
 58         }
 59     }
 60     return false;
 61 }
 62 
 63 bool solve(int st)
 64 {
 65     ans += sc3(st);
 66     if(dfs(st))
 67         return true;
 68     ans.pop_back();
 69     return false;
 70 }
 71 
 72 void Sort()
 73 {
 74     _for(i,0,99)
 75     sort(v[i].begin(),v[i].end());
 76 }
 77 int judge()
 78 {
 79     int odd = 0;
 80     int st = -1;
 81     _for(i,0,60)
 82     {
 83         int sum = 0;
 84         _for(j,0,60)
 85         {
 86             sum += vis[i][j];
 87         }
 88         if(sum&0x1)
 89         {
 90             if(st==-1)
 91                 st = i;
 92             odd ++;
 93         }
 94     }
 95     if(odd!=0 && odd!=2)
 96         return -1;
 97     else if(!odd)
 98         return 0;
 99     else
100         return st;
101 }
102 int main()
103 {
104     N = read();
105 
106     _for(i,0,N)
107     {
108         char c1,c2;
109         cin >> c1 >> c2;
110         v[sc4(c1)].pb(sc4(c2));
111         v[sc4(c2)].pb(sc4(c1));
112         vis[sc4(c1)][sc4(c2)]++;
113         vis[sc4(c2)][sc4(c1)]++;
114     }
115 
116     int st = judge();
117     if(st==-1)
118     {
119         printf("No Solution\n");
120         return 0;
121     }
122     Sort();
123 
124     if(!solve(st))
125         printf("No Solution\n");
126     else
127         cout << ans << endl;
128 
129     return 0;
130 }

 

posted @ 2019-09-11 14:42  Asurudo  阅读(143)  评论(0编辑  收藏  举报