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 ;
}

CodeForces - 508D Tanya and Password(欧拉通路)的更多相关文章

  1. codeforces 508D . Tanya and Password 欧拉通路

    题目链接 给你n个长度为3的子串, 这些子串是由一个长度为n+2的串分割得来的, 求原串, 如果给出的不合法, 输出-1. 一个欧拉通路的题, 将子串的前两个字符和后两个字符看成一个点, 比如acb, ...

  2. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

  3. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

  4. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  5. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  6. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  8. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...

  9. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

随机推荐

  1. java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream(转)

    java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream 使用Tomcat的Manag ...

  2. [转]Geoserver全面了解

    From:http://www.cnblogs.com/ada-zheng/archive/2013/07/16/3192921.html Geoserver是一个功能齐全,遵循OGC开放标准的开源W ...

  3. (转载博文)MFC 窗口句柄获取

    句柄获取方法(获取该窗口的句柄后,即可向该窗口类类发送消息.处理程序):0.获取所在类窗口的句柄: this->m_hwnd 1.主窗口的句柄: 无论在主窗口类内,还是子窗口类内,获取主窗口句柄 ...

  4. Unity doesn't load, no Launcher, no Dash appears

    1. 重新安装 ubuntu-desktop不起作用. Enter the following commands:- Ctrl+Alt+F1 login there by user name and ...

  5. gcc选项-g与-rdynamic的异同

    摘自http://www.tuicool.com/articles/EvIzUn gcc选项-g与-rdynamic的异同 gcc 的 -g ,应该没有人不知道它是一个调试选项,因此在一般需要进行程序 ...

  6. Linux学习笔记6-Linux根目录下各个目录的作用

    /bin:存放最常用命令:  /boot:启动Linux的核心文件: /dev:设备文件: /etc:存放各种配置文件: /home:用户主目录: /lib:系统最基本的动态链接共享库: /mnt:一 ...

  7. 2014 ACM省赛总结

    今年ACM省赛已经过去一个星期左右了,2年的ACM训练是该做个总结了,因为前几日去參加蓝桥杯总决赛,所以没来的及写总结,如今在这小小总结一下吧-- 依晰记得去年省赛时候的样子,如今感觉那时像是个无知的 ...

  8. Hadoop基础

    Hadoop组成 包括两个核心组成:HDFS:分布式文件系统,存储海量的数据MapReduce:并行处理框架,实现任务分解和调度 搭建大型数据仓库,PB级数据的存储.处理.分析.统计等业务(搜索引擎. ...

  9. textarea

    ■ 摘要 项目 说明 形式 <textarea>-</textarea> 支持 H2+ / e2+ / N2+ / Fx1+ / Op6+ / Ch1+ / Sa1+ 标签省略 ...

  10. https配置

    总结了一下: 所谓用HTTPS的时候 app 前端要配置SSL 证书的意思就是:相当于服务器端与app 前端, 一个拿公钥,一个拿私钥.简单得说就是客户端发送请求的时候,用配置好的SSL证书里的加密方 ...