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.
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.
Sample test(s) input5 aca aba aba cab bacoutput
YES abacabainput
4 abc bCb cb1 b13output
NOinput
7 aaa aaa aaa aaa aaa aaa aaaoutput
YES aaaaaaaaa
題目給出n個字符串,要求將這n個字符串連接在一起,要求後綴與前綴(兩個字符)相同,如aba,bac就可以連接abac,然後輸出歐拉路徑。
首先有向圖,整個圖是否連通,歐拉路徑的判斷 僅有1組或0組(入度= 出度+1,出度 = 入度+1) ,其他的入度==出度
對歐拉路徑的輸出問題:
首先找到出度大於入度的點,如果沒有找任意一點。
進行dfs,深搜的過程中修改head[u],保證之前搜過的不會再搜,之後搜過的也不會被搜到:
for(i = head[u] ; i != -1 ; i = head[u] )//後面搜到的不會再搜
{
if( vis[i] == 0 )
{
vis[i] = 1 ;
head[u] = edge[i].next ;//前面搜到的不會再搜
dfs(edge[i].v) ;
}
}
在最後記錄搜索的節點,這樣就能保證歐拉路徑最後的節點一定出現在最後。
#include#include #include #include