CodeForces 508D Tanya and Password(欧拉路径)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=111980

Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

 Status

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

Input
5
aca
aba
aba
cab
bac
Output
YES
abacaba
Input
4
abc
bCb
cb1
b13
Output
NO
Input
7
aaa
aaa
aaa
aaa
aaa
aaa
aaa
Output
YES
aaaaaaaaa
这是有向图的欧拉通路问题,虽然我能看出来,但是输出问题和存储问题没有很好解决。参考了大神们的代码:发现他们用0,1字符和1,2字符作为线段端点的数字信息。妙!输出则是DFS。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N1=2e5+5,N2=4e3; // (26*2+10)^2<4000
int f[N2],in[N2],out[N2],path[N1],e[N2][N2];
bool has[N2];
int len,st,n;
struct node{
    int u,v;
    char name[5];
}snode[N1];
int abs(int x){
    return x>0?x:(-x);
}
void init(){
    for(int i=0;i<N2;i++) f[i]=i;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(path,0,sizeof(path));
    memset(e,0,sizeof(e));
    memset(has,0,sizeof(has));
}
int find(int x){
    if(x==f[x]) return x;
    return f[x]=find(f[x]);
}
int ctoi(char ch){
    if(ch<='9'&&ch>='0') return ch-'0';
    if(ch>='A'&&ch<='Z') return ch-'A'+10;
    return ch-'a'+36;
}
char itoc(int x){
    if(x<=9&&x>=0) return x+'0';
    if(x<=35&&x>=10) return x+'A'-10;
    return x+'a'-36;
}
bool Exist(){
    int t=-1,sum=0,temp;
    for(int i=0;i<N2;i++){
        if(has[i]){
           if(t==-1) t=find(i);
           else if(find(i)!=t)  return 0;
        }
    }
    for(int i=0;i<N2;i++){
        if(has[i]){
             temp=i;
             if(in[i]!=out[i]){
                 sum++;
                 if(abs(in[i]-out[i])>1) return 0;
                 if(out[i]>in[i]) st=i; //链的端点
             }
        }
    }
    if(sum>2) return 0;
    if(sum==0) st=temp;  // 环的一节点
    return 1;
}
void dfs(int q){
    for(int i=0;i<N2;i++){
        while(e[q][i]){
            e[q][i]--;
            dfs(i);
            path[len++]=i;
        }
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int n;
    while(cin>>n){
        init();
        for(int i=0;i<n;i++){
            scanf("%s",snode[i].name);
            snode[i].u=ctoi(snode[i].name[0])*62+ctoi(snode[i].name[1]);
            snode[i].v=ctoi(snode[i].name[1])*62+ctoi(snode[i].name[2]);
            int a=snode[i].u,b=snode[i].v;
            has[a]=has[b]=1;
            f[find(a)]=f[find(b)];
            out[a]++;
            in[b]++;
            e[a][b]++;
        }
        if(!Exist()) puts("NO");
        else {
             len=0;
             dfs(st);
             path[len++]=st;
             puts("YES");
             //cout<<path[len-1]<<endl;
             printf("%c%c",itoc(path[len-1]/62),itoc(path[len-1]%62));
             for(int i=len-2;i>=0;i--){
                 printf("%c",itoc(path[i]%62));
             }
             puts("");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值