原题链接
D. Tanya and Password time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputWhile dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of n + 2 characters. She has written all the possible n three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with n pieces of paper.
Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad's password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct.
InputThe first line contains integer n (1 ≤ n ≤ 2·105), the number of three-letter substrings Tanya got.
Next n lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit.
OutputIf Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO".
If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option.
Examples input 5 aca aba aba cab bac output YES abacaba input 4 abc bCb cb1 b13 output NO input 7 aaa aaa aaa aaa aaa aaa aaa output YES aaaaaaaaa 对于每个字符串,前两个字符为节点,后两个字符为节点,两个节点之间连一条边,最后证明生成的图中是否存在欧拉路即可,并输出路径 #include <bits/stdc++.h> #define maxn 200005 using namespace std; typedef long long ll; char pp[4005][3]; int mp[205][205]; vector<int> v[4005]; int vis[4005], cnt = 0, k1[4005], k2[4005], kk = 0; char str[4], ans[200010]; void dfs(int j){//记录欧拉路径 while(vis[j] < v[j].size()){ dfs(v[j][vis[j]++]); } ans[kk++] = pp[j][1]; } int find(int b){ int j = b; while(j != vis[j]) j = vis[j]; int ss = b; while(vis[ss] != j){ b = vis[ss]; vis[ss] = j; ss = b; } return j; } int main(){ // freopen("in.txt", "r", stdin); int n, from, to; scanf("%d", &n); for(int i = 1; i <= 4000; i++) vis[i] = i; for(int i = 0; i < n; i++){ scanf("%s", str); if(mp[str[0]][str[1]] == 0){ mp[str[0]][str[1]] = ++cnt; pp[cnt][0] = str[0]; pp[cnt][1] = str[1]; from = cnt; } else from = mp[str[0]][str[1]]; if(mp[str[1]][str[2]] == 0){ mp[str[1]][str[2]] = ++cnt; pp[cnt][0] = str[1]; pp[cnt][1] = str[2]; to = cnt; } else to = mp[str[1]][str[2]]; v[from].push_back(to); k2[from]++; k1[to]++; int b1 = find(from), b2 = find(to); if(b1 != b2) vis[b1] = b2; } int c = 0; for(int i = 1; i <= cnt; i++) if(vis[i] == i){ c++; } if(c > 1){ puts("NO"); return 0; } int s1 = -1, s2 = -1, f = 0; for(int i = 1; i <= cnt; i++){ if(abs(k1[i] - k2[i]) > 1){ puts("NO"); return 0; } if(k1[i] > k2[i]){ s1 = i; f++; } if(k2[i] > k1[i]){ s2 = i; f++; } } if(f > 2){ puts("NO"); return 0; } if(s1 == -1 && s2 == -1){ s2 = 1; puts("YES"); } else if(s1 != -1 && s2 != -1) puts("YES"); memset(vis, 0, sizeof(vis)); dfs(s2); printf("%c", pp[s2][0]); for(int i = kk - 1; i >= 0; i--) printf("%c", ans[i]); puts(""); return 0; }