Address:http://www.luogu.org/problem/show?pid=1341 Topic Description
Given n each of the different unordered pairs of letters (case-insensitive, the two letters in a pair of letters can be reversed). Construct a string with a n+1 letter so that each letter pair appears in the string.
Input/output format
Input format:
The first line enters a positive integer n.
The following n lines are two letters per line, indicating that the two letters need to be contiguous.
Output format:
The output string that satisfies the requirement.
If there are no strings to meet the requirements, output "no solution".
If there are multiple scenarios, please output the preceding letters with the ASCII encoding as small as possible (the dictionary order is minimal) scheme
Input/Output sample
Input Sample # #:
4aZtZXtaX
Sample # # of output:
Description
"Data size and conventions"
The number of different disordered letters is limited, and the size of n can be calculated.
Ideas
It is easy to see that this is a Eulerian graph. However, I thought for a long while will not write a graph algorithm, had to use deep search and write back, but the data is in the water, unexpectedly!
P1: I used the judgment of the heavy side.
P2: The dictionary order is required to output, then the search time from the smallest dictionary order to start the search.
varA:Array['A'..'Z','A'..'Z'] ofLongint; Du:Array['A'..'Z'] ofLongint; Ans:Array[0..100000] ofChar; N:longint;procedureDFS (u:char;tot:longint);varJ:longint;k:char;beginAns[tot]:=u; iftot=n+1 Then begin forj:=1 ton+1 Dowrite (ans[j]); Writeln; Halt End; fork:='A' to 'Z' Do ifA[u,k]<>0 Then beginDec (a[u,k]); Dec (A[k,u]); DFS (K,tot+1); Inc (A[k,u]); Inc (A[u,k]); //BacktrackingEnd;End;procedureChange ;beginFillchar (A,sizeof (a),0); Fillchar (du,sizeof (du),0);End;procedureInit;varI:longint;one,two:char;beginREADLN (n); fori:=1 toN Do beginreadln (One,two); Inc (A[one,two]); Inc (A[two,one]); Inc (Du[one]); Inc (Du[two]); End;End;proceduremain;varX:char;one,two:char;begin forx:='A' to 'Z' Do if(Du[x]MoD 2=1) Then begin ifTwo<>' ' Then beginWriteln ('No Solution'); Halt End; if(one<>' ') and(two=' ') Thentwo:=x; ifOne=' ' Thenone:=x; End; if(one<>' ') and(two=' ') Then beginWriteln ('No Solution'); Halt End; //One and two are two alternate strings, given the possibility of a heavy edgeif(one=' ') and(two=' ') Then begin forx:='A' to 'Z' Do ifDu[x]<>0 ThenBreak ; DFS (x,1); End Else ifOne>two ThenDfs (both,1) ElseDfs (one,1); //Everything for the dictionary OrderEnd;beginChange ; Init Main;End.View Code
Unordered letter Pairs