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.
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.
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.
5 aca aba aba cab bac
YES abacaba
4 abc bCb cb1 b13
NO
7 aaa aaa aaa aaa aaa aaa aaa
YES aaaaaaaaa
例如abc分为ab和bc两个部分,然后引一条从ab指向bc的边。由于每一个节点都是由两个字符组成,可以将其转化成十进制数加以区分,则可以用来表示不同的节点。
//
// main.cpp
// Tanya and Password
//
// Created by 蘇與軒 on 15/1/29.
// Copyright (c) 2015年 蘇與軒. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#define rep(i,a,b) for (int i=a;i<(b+1);i++)
#define Rep(i,a,b) for (int i=a;i>=b;i--)
#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();e++)
#define mid ((l+r)>>1)
#define lson (k<<1)
#define rson (k<<1|1)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
vector<pii> G[500050];
int deg[500050],tot=0,a[500050],cur[500050];
bool vis[500050];
int n;
char str[200050][5];
int ch(char ch) {
if (ch<='z'&&ch>='a') return ch-'a';
if (ch<='Z'&&ch>='A') return ch-'A'+26;
return ch-'0'+52;
}
int getx(int m,int p) {
return ch(str[m][p])*62+ch(str[m][p+1]);
}
void eur(int s,int p) {
int f;
while (cur[s]<G[s].size()) {
f=cur[s]++;
if (vis[G[s][f].second]==false){
vis[G[s][f].second]=true;
eur(G[s][f].first,G[s][f].second);
}
}
a[tot++]=p;
}
bool judge(){
int x=0,y=0,pos=getx(1,0);
rep(i,0,4000) {
if (deg[i]>1||deg[i]<-1) return false;
if (deg[i]==1) x++,pos=i;
if (deg[i]==-1) y++;
}
if (x!=y||x>1) return false;
eur(pos,-1);
tot--;
return tot>=n;
}
int main(int argc, const char * argv[]) {
scanf("%d",&n);
memset(vis,0,sizeof vis);
rep(i,1,n){
scanf("%s",str[i]);
int x=getx(i,0);int y=getx(i,1);
G[x].push_back(pii(y,i));
deg[x]++;deg[y]--;
}
if (!judge()) puts("NO");
else{
puts("YES");
printf("%s",str[a[tot-1]]);
Rep(i,tot-2,0) printf("%s",str[a[i]]+2);
puts("");
}
return 0;
}