catenyms
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 11214 |
|
Accepted: 2908 |
Description
A catenym is a pair of words separated by a period such that the last letter of the first word was the same as the last let ter of the second. For example, the following is catenyms:
Dog.gopher
Gopher.rat
Rat.tiger
Aloha.aloha
Arachnid.dog
A compound catenym is a sequence of three or more words separated by periods such so each adjacent pair of words forms a Catenym. For example,
Aloha.aloha.arachnid.dog.gopher.rat.tiger
Given A dictionary of lower case words, you is to find a compound catenym This contains each of the words exactly once.
Input
The first line of standard input contains T, the number of test cases. Each test case is begins with 3 <= n <= 1000-the number of words in the dictionary. n distinct dictionary words follow; Each of the word is a string of between 1 and lowercase letters in a line by itself.
Output
For each test case, output a line giving the lexicographically least compound catenym that contains each dictionary word E Xactly once. Output "* * *" If there is no solution.
Sample Input
26alohaarachniddoggopherrattiger3oakmapleelm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger***
Source
Waterloo Local 2003.01.25 province race that the smallest spanning tree template set a long time has been wrong, just feel like just holding some of their own familiar with the diagram of the template, in fact, nothing, the foundation escaped not solid. So still feel good practice again, starting from the graph theory, from the thought has been back but the actual will not be the European pull loop to start it. Although I do not know how long I can persist ... It's hard work anyway. Approximate test instructions: given some strings, it is required to concatenate all the strings like the idiom solitaire, if there are many cases, the output of the smallest dictionary order. The problem of a Euler road, because the requirements of the dictionary output, so the input string must be sorted by dictionary ordering (do this to know that the string type can be directly ordered, the result is the dictionary order, sure enough to learn). Then a,b,c such as vertices with each string as the edge, from the first letter to the tail letter, (the beginning of each word as the vertex is always not, after looking at the puzzle to change). After building a map is a direct Euler loop problem, the first to determine whether the unicom or not and can constitute Eulerian graph, and then the path output.
#include <cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<vector>using namespacestd;intn,vis[1005],len[1005],k,inch[1005], out[1005],ans[1005],cnt;strings[1005];structnode{intu,m;}; Vector<node> g[1005];voidEulerintx) { for(intI=0; I<g[x].size (); i++){ intv=g[x][i].u; if(!vis[G[X][I].M]) {vis[G[X][I].M]=1; Euler (v); CNT++; ANS[CNT]=g[x][i].m; } }}intMain () {intT scanf"%d",&t); while(t--){ intstart= -; CIN>>N; K=0; memset (Vis,0,sizeof(VIS)); memset (inch,0,sizeof(inch)); memset ( out,0,sizeof( out)); for(intI=1; i<= -; i++) g[i].clear (); for(intI=1; i<=n;i++) {cin>>S[i]; } sort (S+1, s+n+1); for(intI=1; i<=n;i++) {Len[i]=s[i].size (); } for(intI=1; i<=n;i++) {node T; T.u=s[i][len[i]-1]-'a'+1; t.m=i; g[s[i][0]-'a'+1].push_back (t); out[s[i][0]-'a'+1]++;inch[s[i][len[i]-1]-'a'+1]++; Start=min (start,s[i][0]-'a'+1); Start=min (start,s[i][len[i]-1]-'a'+1); } intfin=0, fout=0, end=0, flag=0; for(intI=1; i<= -; i++){ if(inch[i]+1== out[i]) {Start=i; fin++; } Else if(inch[i]== out[i]+1) {Fout++; } Else if(inch[i]!= out[i]) flag=1; } if(flag==1) {printf ("***\n");Continue; } Else if((fin==1&&fout==1) || (fin==0&&fout==0) ) {CNT=0; Euler (start); if(cnt==N) { for(inti=cnt;i>=1; i--) { if(i==cnt) cout <<s[Ans[i]]; Elsecout <<"."<<s[Ans[i]]; } printf ("\ n"); } Else{printf ("***\n");Continue; } } Else{printf ("***\n");Continue; } } return 0;}
Psong
POJ 2337 Euro-La route