Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9832 | Accepted: 2561 |
Description
dog.gopher gopher.rat rat.tiger aloha.aloha arachnid.dog
Input
Output
Sample Input
2 6 aloha arachnid dog gopher rat tiger 3 oak maple elm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger *** 代码:#include <iostream> #include <cmath> #include <cstdio> #include <cstring> using namespace std; int degreein[26],degreeout[26],d[1005]; bool visit[1005]; struct deg{ int start; int end; char w[30]; }deg_order[1005]; int num; int cmp(const void *a,const void *b) { deg *c=(deg *)a; deg *d=(deg *)b; if(c->start!=d->start) return c->start-d->start; else return strcmp(c->w,d->w); } int Abs(int a) { if(a>0) return a; else return -a; } int getsource()//寻找起点 { int i; int source,cnt1=0,cnt2=0; for(i=0;i<26;i++) { if(Abs(degreein[i]-degreeout[i])==2) return -1; else if(degreein[i]-degreeout[i]==1) { cnt1++;//基度顶点加1 } else if(degreein[i]-degreeout[i]==-1) { cnt2++;//基度顶点加1 source=i; } } if(cnt1>1||cnt2>1)//如果基度顶点数大于一说明不能构成欧拉路径 return -1; if(cnt1==0)//如果不存在基度顶点 { int i; for(i=0;i<26;i++) if(degreeout[i]) return i; } else //找到起始点 return source; } bool dfs(int source,int cnt)//搜索是否存在欧拉路径 { int i; if(cnt==num) return true; for(i=0;i<num;i++) { if(visit[i]||deg_order[i].start<source) continue; else if(deg_order[i].start>source) return false; d[cnt]=i; visit[i]=1; if(dfs(deg_order[i].end,cnt+1)) return true; visit[i]=0; } return false; } int main() { int t; cin>>t; while(t--) { memset(visit,0,sizeof(visit)); memset(degreein,0,sizeof(degreein)); memset(degreeout,0,sizeof(degreeout)); cin>>num; int i; for(i=0;i<num;i++) { cin>>deg_order[i].w; deg_order[i].start=deg_order[i].w[0]-'a'; deg_order[i].end=deg_order[i].w[strlen(deg_order[i].w)-1]-'a'; degreein[deg_order[i].end]++; degreeout[deg_order[i].start]++; } int source=getsource(); if(source==-1) { cout<<"***"<<endl; continue; } qsort(deg_order,num,sizeof(deg_order[0]),cmp); if(!dfs(source,0)) { cout<<"***"<<endl; continue; } int j; cout<<deg_order[d[0]].w; for(j=1;j<num;j++) cout<<'.'<<deg_order[d[j]].w; cout<<endl; } return 0; }