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. UI原则之-拼车

    1.简洁------------一目了然,简洁明了 2.易用------------操作方便 3.直观.快速-------快速定位到所需信息 4.友好-------网络延时.超时.等待 5.易懂--- ...

  2. LINUX 暂停、继续进程

    LINUX 暂停.继续进程 kill -STOP 1234 将该进程暂停. 如果要让它恢复到后台,用kill -CONT 1234 (很多在前台运行的程序这样是不行的) 如果要恢复到前台,请在当时运行 ...

  3. (译)linux系统关于命令echo的15个例子

    15 Practical Examples of ‘echo’ command in Linux By Avishek Kumar Under: Linux Commands On: August 2 ...

  4. PHP实现好友生日邮件提醒

    我有一个想法是这样的,希望每天知道今天是我哪位好友的生日? 当然,我得首先保存我所有好友的生日信息 实现:在新浪申请免费服务器,并申请Mysql应用,然后建一张表保存好友的信息 我想,我已经完成第一步 ...

  5. Oracle 表的常见操作

    --创建表并指定表空间 create table goods( id VARCHAR2(20) primary key, name VARCHAR2(20) NOT NULL, price NUMBE ...

  6. 网易云课堂_C++程序设计入门(上)_第1单元:C++概览_第1单元作业 - 写代码 - 互评 (难度:易)

    第1单元作业 - 写代码 - 互评 (难度:易) 查看帮助 返回   提交作业(截止时间已过) 完成并提交作业     作业批改 互评训练   互评作业   自评作业     成绩公布 查看成绩 温 ...

  7. OC基础11:基本的C语言特性2

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 19.定义一个int类型的指针: int ...

  8. 【Android Training UI】创建自定义Views(Lesson 2 - 自定义Drawing)

    发布在我的网站:http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-2 ...

  9. 第九十六题(编写strcpy 函数)

    96.08 年中兴校园招聘笔试题 1.编写strcpy 函数 已知strcpy 函数的原型是 char *strcpy(char *strDest, const char *strSrc); 当中st ...

  10. Banner 切换

    在线项目 :  Banner 切换 时间 : 2个小时 (15:00 - 17:00)满分 : 100分------------------------------------------------ ...