CodeForces - 508D Tanya and Password(欧拉通路)
Description
While 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.
Input
The 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.
Output
If 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.
Sample Input
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
题意:给一个n,输入n个长度为3的字符串,字符包含英文的大小写和数字,判断是否存在一个长度为n+2的字符串包含全部n个子串
思路:题目分析:可以把问题转化为判断是否存在欧拉通路,那如何构图?因为题目说了每个字符串的长度为3,因此我们把它的前两个字符当做一个结点,后两个字符当作一个结点,然后构建一个有向图,只需要判断这张图是否存在欧拉通路即可,欧拉通路是一条经过图(无向图或有向图)中所有边一次且仅一次行遍图中所有顶点的通路。这题和poj2377有点类似,但是这题有两点比那题棘手,1是结点的存储问题,那题是尾首字符相同便可链接,而这题需要将结点散列,因为一共就10+26+26=62个字符,我们先将62个字符从1到61编号再把结点设置为62x+y的整型变量,则可以将所有结点表示出来。还有个麻烦的地方在于数据量,本题的n即边数达到20w,如果已经判断存在欧拉通路,按照点dfs来找的话,平行边和自环一多很有可能超时甚至爆栈,因此我们可以用边来找,用边找的好处是找的过程中可以把自环和平行的都去掉,下面就是如何判断是否存在欧拉通路的问题
.出入度都相等则为欧拉回路,dfs起点任意,若出入度相差1的点的个数不大于2则把出度大的做为起点,若个数大于2或者出入度相差超多1则不存在欧拉通路
先hash一下每一个点
然后确定起点和终点,起点就是出度比入度大一的点
终点就是入度比出度大一的位置
然后我们再dfs一发起点就好了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 #include <stack> 15 using namespace std; 16 #define PI acos(-1.0) 17 #define max(a,b) (a) > (b) ? (a) : (b) 18 #define min(a,b) (a) < (b) ? (a) : (b) 19 #define ll long long 20 #define eps 1e-10 21 #define MOD 1000000007 22 #define N 300000 23 #define inf 1e12 24 int n; 25 string s,ans; 26 vector<int> edge[N]; 27 int in[N],out[N],cnt[N]; 28 void dfs(int i){ 29 while(cnt[i]<edge[i].size()){ 30 dfs(edge[i][cnt[i]++]); 31 } 32 ans+=(char)i%256; 33 } 34 35 int main() 36 { 37 while(scanf("%d",&n)==1){ 38 int u,v; 39 for(int i=0;i<n;i++){ 40 cin>>s; 41 u=s[0]*256+s[1]; 42 v=s[1]*256+s[2]; 43 edge[u].push_back(v); 44 in[v]++; 45 out[u]++; 46 } 47 int start=u; 48 int l=0,r=0; 49 int flag=1; 50 for(int i=0;i<N;i++){ 51 int d=in[i]-out[i]; 52 if(d==-1){ 53 l++; 54 start=i; 55 }else if(d==1){ 56 r++; 57 }else if(d!=0){ 58 printf("NO\n"); 59 flag=0; 60 break; 61 } 62 if(l>1 || r>1){ 63 printf("NO\n"); 64 flag=0; 65 break; 66 } 67 } 68 if(flag==0){ 69 continue; 70 } 71 dfs(start); 72 ans+=(char)(start/256); 73 reverse(ans.begin(),ans.end()); 74 if(ans.length()!=n+2){ 75 printf("NO\n"); 76 }else{ 77 printf("YES\n"); 78 cout<<ans<<endl; 79 } 80 } 81 return 0; 82 }