Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路

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[20];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

vector<int> edges[66000];
int in[66000],out[66000],cnt[66000];
string ans;

void dfs(int i)
{
    while(cnt[i]<edges[i].size())
    {
        dfs(edges[i][cnt[i]++]);
    }
    ans+=(char)i%256;
}
int main()
{
    //test;
    int n,u,v,start=0,i;
    scanf("%d",&n);
    string s;
    for( i=0;i<n;++i)
    {
        cin>>s;
        u=s[0]*256+s[1];
        v=s[1]*256+s[2];
        edges[u].push_back(v);
        in[u]++;  
        out[v]++;
    }
    start=u;
    int l=0,r=0;
    for(i=0;i<66000;++i)
    {
        int d=in[i]-out[i];
        if(d==1){ l++; start=i; }
        else if(d==-1) r++;
        else if(d!=0)
        {
            printf("NO\n");
            return 0;
        }
        if(l>1 || r>1)
        {
            printf("NO\n");
            return 0;
        }
    }
    dfs(start);
    ans+=(char)(start/256);
    reverse(ans.begin(),ans.end());
    if(ans.length()!=n+2) 
        printf("NO\n");
    else 
        cout<<"YES\n"<<ans<<endl;
    return 0;
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4563065.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值