• 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
    ");
                return 0;
            }
            if(l>1 || r>1)
            {
                printf("NO
    ");
                return 0;
            }
        }
        dfs(start);
        ans+=(char)(start/256);
        reverse(ans.begin(),ans.end());
        if(ans.length()!=n+2) 
            printf("NO
    ");
        else 
            cout<<"YES
    "<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    php 导出csv文件
    dns 服务器配置
    ettercap ARP dns 欺骗
    for循环内 执行$ajax(){}
    js 如何生成二维数组
    jquery读取csv文件并用json格式输出
    echo 换行
    cmd命令运行php,php通过cmd运行文件
    Git 常用命令整理
    图像裁剪插件
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4563065.html
Copyright © 2020-2023  润新知