codeforces Round#288D Tanya and Password 欧拉通路

D. Tanya and Password
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
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
解法:其实很简单,把长度为3的字符串分为前后两个部分,然后每个部分单独当做一个节点,可以得到一张图。在该图之中找一条欧拉通路即可,不过需注意需要使用当前弧对深搜进行优化,不然的话,会TLE

例如abc分为ab和bc两个部分,然后引一条从ab指向bc的边。由于每一个节点都是由两个字符组成,可以将其转化成十进制数加以区分,则可以用来表示不同的节点。

//
//  main.cpp
//  Tanya and Password
//
//  Created by 蘇與軒 on 15/1/29.
//  Copyright (c) 2015年 蘇與軒. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#define rep(i,a,b) for (int i=a;i<(b+1);i++)
#define Rep(i,a,b) for (int i=a;i>=b;i--)
#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();e++)
#define mid ((l+r)>>1)
#define lson (k<<1)
#define rson (k<<1|1)
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
vector<pii> G[500050];
int deg[500050],tot=0,a[500050],cur[500050];
bool vis[500050];
int n;
char str[200050][5];
int ch(char ch) {
    if (ch<='z'&&ch>='a')   return ch-'a';
    if (ch<='Z'&&ch>='A')   return ch-'A'+26;
    return ch-'0'+52;
}
int getx(int m,int p) {
    return ch(str[m][p])*62+ch(str[m][p+1]);
}
void eur(int s,int p) {
    int f;
    while (cur[s]<G[s].size()) {
        f=cur[s]++;
        if (vis[G[s][f].second]==false){
            vis[G[s][f].second]=true;
            eur(G[s][f].first,G[s][f].second);
        }
    }
    a[tot++]=p;
}
bool judge(){
    int x=0,y=0,pos=getx(1,0);
    rep(i,0,4000) {
        if (deg[i]>1||deg[i]<-1)    return false;
        if (deg[i]==1)  x++,pos=i;
        if (deg[i]==-1) y++;
    }
    if (x!=y||x>1)  return false;
    eur(pos,-1);
    tot--;
    return tot>=n;
}
int main(int argc, const char * argv[]) {
    scanf("%d",&n);
    memset(vis,0,sizeof vis);
    rep(i,1,n){
        scanf("%s",str[i]);
        int x=getx(i,0);int y=getx(i,1);
        G[x].push_back(pii(y,i));
        deg[x]++;deg[y]--;
    }
    if (!judge())   puts("NO");
    else{
        puts("YES");
        printf("%s",str[a[tot-1]]);
        Rep(i,tot-2,0)  printf("%s",str[a[i]]+2);
        puts("");
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值