D. Tanya and Password

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/508/problem/D

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

Input

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.

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

5
aca
aba
aba
cab
bac

Sample Output

YES
abacaba

HINT

题意

题解:

先hash一下每一个点

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

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

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

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** vector<int> edges[];
int in[],out[],cnt[];
string ans; void dfs(int i)
{
while(cnt[i]<edges[i].size())
{
dfs(edges[i][cnt[i]++]);
}
ans+=(char)i%;
}
int main()
{
//test;
int n,u,v,start=,i;
scanf("%d",&n);
string s;
for( i=;i<n;++i)
{
cin>>s;
u=s[]*+s[];
v=s[]*+s[];
edges[u].push_back(v);
in[u]++;
out[v]++;
}
start=u;
int l=,r=;
for(i=;i<;++i)
{
int d=in[i]-out[i];
if(d==){ l++; start=i; }
else if(d==-) r++;
else if(d!=)
{
printf("NO\n");
return ;
}
if(l> || r>)
{
printf("NO\n");
return ;
}
}
dfs(start);
ans+=(char)(start/);
reverse(ans.begin(),ans.end());
if(ans.length()!=n+)
printf("NO\n");
else
cout<<"YES\n"<<ans<<endl;
return ;
}

Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路的更多相关文章

  1. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...

  2. Codeforces Round #565 (Div. 3)--D. Recover it!--思维+欧拉筛

    D. Recover it! Authors guessed an array aa consisting of nn integers; each integer is not less than ...

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

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

  4. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  5. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  6. Codeforces Round #288 (Div. 2)

    A. Pasha and Pixels     题意就是给一个n*m的矩阵,k次操作,一开始矩阵全白,一次操作可以染黑一个格子,问第几次操作可以使得矩阵中存在一个2*2的黑色矩阵.直接模拟即可 代码: ...

  7. 欧拉回路/通路 Codeforces Round #288 (Div. 2)

    http://codeforces.com/contest/508/problem/D 以上是题目链接 题目大意 给n个字符串看能不能链接在一起 因为 三个三个分割 所以字符串 如abc ab作为起点 ...

  8. Codeforces Round #346 (Div. 2) C Tanya and Toys

    C. Tanya and Toys 题目链接http://codeforces.com/contest/659/problem/C Description In Berland recently a ...

  9. Codeforces Round #346 (Div. 2) C. Tanya and Toys 贪心

    C. Tanya and Toys 题目连接: http://www.codeforces.com/contest/659/problem/C Description In Berland recen ...

随机推荐

  1. 骑士周游问题 --- 递归解法 --- java代码

    骑士游历: 定义了向量的数组M,行数组X,列数组Y, 棋盘plane,计数器count,走动步数step 需要注意的是,递归函数的进入前的验证,原先的想法是传入来时的方向参数,可是这样的想法被实践否定 ...

  2. 理解KMP

    KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n),KMP匹配算法,可以证明它的时间复杂度为O(m+n).. 一.简单匹配算法 先来看一个 ...

  3. 时间日期Date类型

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  4. Java的平台无关性

    转载自:http://www.cnblogs.com/Y/archive/2011/03/22/JavaVM_Learning_Chapter2_Platform_Independence.html ...

  5. arm tiny6410双网卡桥接问题

    这几天做实验,想用arm tiny6410板子做个mesh网络节点.该板子本身自带一个lan网卡,我自己配了一个tp-link的usb无线网卡.其中wlan网卡工作在adhoc模式下,作为mesh骨干 ...

  6. vmware下ubuntu14.04调整分辨率

    很多人在vmware中安装ubuntu时,为了调整屏幕分辨率,都去下载并安装vmware-tools.其实,这是没有必要的.如果你需要vmware和宿主机实现共享,或者为了使文件能拖进拖出,再或者是需 ...

  7. php 解压 .gz 文件

    在百度搜索到的 PharData 类和 ZipArchive 都是解压不了 .gz 的文件的,后来在 google 搜索到解决方法,问题解决. try { $phar = new PharData($ ...

  8. ZOJ-3349 Special Subsequence 线段树优化DP

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3349 题意:给定一个数列,序列A是一个满足|Ai-Ai-1| & ...

  9. html5 canvas图片反色

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 关于文章“cocos2dx移植android平台-我的血泪史”需要注意事项

    关于文章"cocos2dx移植android平台-我的血泪史"需要注意事项 在上次转载的这篇文章中,按照配置一步一步的下去.发现工程中在Android.mk中有一处错误.直接bui ...