[codeforces 508D]Tanya and Password

time limit per test : 2 seconds
memory limit per test : 256 megabytes

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+2n+2n+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)n (1 ≤ n ≤ 2·10^5)n(1n2105), 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.

Examples
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

题意:
给定n个长度为3的字符串,要求出是否存在一个长度为n+2字符串s,使得那n个字符串可以按照某个顺序拼起来成为s。
拼接方式为:如果字符串a可以和字符串b拼接,那么a的后两位一定等于b的前两位。拼接完的字符串为a+(b的第三个字符)

题解:
我对内存的掌控真是无与伦比.jpg
首先我们考虑搜索。
我们把每个长度为3的字符串拆成两个部分,前两个字符和后两个字符,每一种状态用图上的一个点来表示,在他们两之间连一条单向边,然后对于每个点记录一下它的纯出边的数量(出边-入边)。
然后接下来的问题就是查找起点。
这个起点必须是唯一的,并且它的纯出边的数量必须为1,而且不能存在纯出边数量大于1的点,如果不存在这种点的话,就随便选取一个点作为起点(整张图有可能是个简单环)。
然后进行dfs,每次任意选取一个出边直接dfs即可。然后回溯的时候记录答案,最后反转一下序列就行了。最后输出的时候还要确认一下长度是不是n+2n+2n+2
不能直接在dfs的开头进行记录答案,否则会运行错误,因为可能有很多种情况都被塞到了答案序列里面

#include<bits/stdc++.h>
#define pa pair<int,int>
#define MAXN 256*256
using namespace std;
vector<int>tr[70004];
pa node[200004];
int n,cnt,ru[70004];
char ans[400004];
int encode(int x,int y){
    return x*256+y;
}
void dfs(int x){
    while(tr[x].size()){
        int p=tr[x].back();
        tr[x].pop_back();
        dfs(p);
    }
    ans[++cnt]=x%256;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        char s[6];
        scanf("%s",s+1);
        node[i]=make_pair(encode(s[1],s[2]),encode(s[2],s[3]));
        //cout<<encode(s[1],s[2])<<" "<<encode(s[2],s[3])<<endl;
        ++ru[node[i].first];
        --ru[node[i].second];
        tr[node[i].first].push_back(node[i].second);
    }
    int st=0,cp=0;
    for(int i=0;i<=MAXN;i++){
        if(ru[i]>0){
            cp++;
            st=i;
        }
    }//cout<<cp<<" "<<st<<endl;
    if(cp>1||ru[st]>1)return puts("NO"),0;
    if(cp==0){
        for(int i=0;i<=MAXN;i++)if(tr[i].size())st=i;
    }
    cnt=0;
    ans[++cnt]=st/256;
    dfs(st);
    reverse(ans+2,ans+cnt+1);
    if(cnt!=n+2)return puts("NO"),0;
    else puts("YES");
    for(int i=1;i<=n+2;i++)printf("%c",ans[i]);
    puts("");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值