Description

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 +  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.

Input

The first line contains integer n ( ≤ n ≤ ·), 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.

Output

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.

Sample Input

Input
aca
aba
aba
cab
bac
Output
YES
abacaba
Input
abc
bCb
cb1
b13
Output
NO
Input
aaa
aaa
aaa
aaa
aaa
aaa
aaa
Output
YES
aaaaaaaaa

题意:给一个n,输入n个长度为3的字符串,字符包含英文的大小写和数字,判断是否存在一个长度为n+2的字符串包含全部n个子串

思路:题目分析:可以把问题转化为判断是否存在欧拉通路,那如何构图?因为题目说了每个字符串的长度为3,因此我们把它的前两个字符当做一个结点,后两个字符当作一个结点,然后构建一个有向图,只需要判断这张图是否存在欧拉通路即可,欧拉通路是一条经过图(无向图或有向图)中所有边一次且仅一次行遍图中所有顶点的通路。这题和poj2377有点类似,但是这题有两点比那题棘手,1是结点的存储问题,那题是尾首字符相同便可链接,而这题需要将结点散列,因为一共就10+26+26=62个字符,我们先将62个字符从1到61编号再把结点设置为62x+y的整型变量,则可以将所有结点表示出来。还有个麻烦的地方在于数据量,本题的n即边数达到20w,如果已经判断存在欧拉通路,按照点dfs来找的话,平行边和自环一多很有可能超时甚至爆栈,因此我们可以用边来找,用边找的好处是找的过程中可以把自环和平行的都去掉,下面就是如何判断是否存在欧拉通路的问题

.出入度都相等则为欧拉回路,dfs起点任意,若出入度相差1的点的个数不大于2则把出度大的做为起点,若个数大于2或者出入度相差超多1则不存在欧拉通路

先hash一下每一个点

然后确定起点和终点,起点就是出度比入度大一的点

终点就是入度比出度大一的位置

然后我们再dfs一发起点就好了

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 300000
#define inf 1e12
int n;
string s,ans;
vector<int> edge[N];
int in[N],out[N],cnt[N];
void dfs(int i){
while(cnt[i]<edge[i].size()){
dfs(edge[i][cnt[i]++]);
}
ans+=(char)i%;
} int main()
{
while(scanf("%d",&n)==){
int u,v;
for(int i=;i<n;i++){
cin>>s;
u=s[]*+s[];
v=s[]*+s[];
edge[u].push_back(v);
in[v]++;
out[u]++;
}
int start=u;
int l=,r=;
int flag=;
for(int i=;i<N;i++){
int d=in[i]-out[i];
if(d==-){
l++;
start=i;
}else if(d==){
r++;
}else if(d!=){
printf("NO\n");
flag=;
break;
}
if(l> || r>){
printf("NO\n");
flag=;
break;
}
}
if(flag==){
continue;
}
dfs(start);
ans+=(char)(start/);
reverse(ans.begin(),ans.end());
if(ans.length()!=n+){
printf("NO\n");
}else{
printf("YES\n");
cout<<ans<<endl;
}
}
return ;
}

最新文章

  1. xshell有大量打印时,显示信息不全
  2. iOS之开发小技巧
  3. Office 365 - SharePoint 2013 Online 中创建母版页
  4. H5课程大纲
  5. common-pool2对象池(连接池)的介绍及使用
  6. Majority Element
  7. grep sed
  8. WCF Rest Json
  9. MultipeerConnectivity
  10. HTML 5 &lt;details&gt; 标签
  11. SOCKET网络编程细节问题(4)
  12. log4net 开箱即用
  13. router-link RangeError: Maximum call stack size exceeded
  14. 面试:atoi() 与 itoa()函数的内部实现(转)
  15. MyEclipse和eclipse的区别
  16. 作业三:LINUX内核的启动过程
  17. Dubbo的一些编码约定和设计原则
  18. MongoDB整库备份+整库导入
  19. Oracle中用户解锁
  20. Vue 获取数据、事件对象、todolist

热门文章

  1. android Xml生成一条细线,以及获取屏幕宽度和高度
  2. 评价早期SaaS创业公司时,投资人在关注什么?(是否有机会发展成一个平台,长期的护城河)
  3. haproxy image跳转 haproxy匹配 匹配到了就停止,不会继续往下匹配
  4. 强烈推荐一款CSS导航菜单
  5. OSCHina技术导向:Java轻量web开发框架——JFinal
  6. Binary Tree Level Order Traversal - leetcode - java
  7. [网络分析]WEBQQ3.0协议分析---good good study
  8. Mysql 多表查询
  9. mac 安装maven 和改动java环境变量
  10. Lucene 4.4 依据Int类型字段删除索引