程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> codeforecs

codeforecs

編輯:關於C++
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

題目給出n個字符串,要求將這n個字符串連接在一起,要求後綴與前綴(兩個字符)相同,如aba,bac就可以連接abac,然後輸出歐拉路徑。

首先有向圖,整個圖是否連通,歐拉路徑的判斷 僅有1組或0組(入度= 出度+1,出度 = 入度+1) ,其他的入度==出度

對歐拉路徑的輸出問題:

首先找到出度大於入度的點,如果沒有找任意一點。

進行dfs,深搜的過程中修改head[u],保證之前搜過的不會再搜,之後搜過的也不會被搜到:

for(i = head[u] ; i != -1 ; i = head[u] )//後面搜到的不會再搜
{
if( vis[i] == 0 )
{
vis[i] = 1 ;
head[u] = edge[i].next ;//前面搜到的不會再搜
dfs(edge[i].v) ;
}
}

在最後記錄搜索的節點,這樣就能保證歐拉路徑最後的節點一定出現在最後。

#include 
#include 
#include 
#include 
#include 
using namespace std ;
//歐拉路徑:每個邊只經過一次,如果是一個圈,稱為歐拉回路
struct node
{
    int v ;
    int next ;
} edge[2100000];
int head[2000000] , cnt , vis[2000000] ;
int in[100000] , out[100000] ,  p_num , c[2100000] ;
void add(int u,int v)
{
    edge[cnt].v = v ;
    edge[cnt].next = head[u] ;
    head[u] = cnt++ ;
    return ;
}
void dfs(int u)
{
    int i , v ;
    for(i = head[u] ; i != -1 ; i = head[u] )
    {
        if( vis[i] == 0 )
        {
            vis[i] = 1 ;
            head[u] = edge[i].next ;
            dfs(edge[i].v) ;
        }
    }
    c[p_num++] = u ;
}
int main()
{
    int n , i , j , u , v ;
    char str[10] ;
    memset(head,-1,sizeof(head)) ;
    memset(vis,-1,sizeof(vis)) ;
    cnt = 0 ;
    p_num = 0 ;
    scanf("%d", &n) ;
    for(i = 0 ; i < n ; i++)
    {
        scanf("%s", str) ;
        u = str[0]*1000+str[1] ;
        v = str[1]*1000+str[2] ;
        add(u,v) ;
        if( vis[u] == -1 )
        {
            vis[u] = p_num ;
            c[p_num++] = u ;
        }
        if( vis[v] == -1 )
        {
            vis[v] = p_num ;
            c[p_num++] = v ;
        }
        in[ vis[u] ]++ ;
        in[ vis[v] ]-- ;
    }
    int flag1 = 0 , flag2 = 0 ;
    for(i = 0 ; i < p_num ; i++)
    {
        if( in[i] < -1 || in[i] > 1 ) break ;
        if( in[i] == 1 )
            flag1++ ;
        if( in[i] == -1 )
            flag2++ ;
    }
    if( i < p_num || !( flag1 == flag2  && flag1 <= 1 ) )
    {
        printf("NO\n") ;
        return 0;
    }
    u = c[0] ;
    for(i = 0 ; i < p_num ; i++)
        if( in[i] == 1 )
            u = c[i] ;
    p_num = 0 ;
    memset(vis,0,sizeof(vis)) ;
    dfs(u) ;
    if( p_num < n+1 )
    {
        printf("NO\n") ;
        return 0 ;
    }
    printf("YES\n") ;
    printf("%c%c", c[p_num-1]/1000 , c[p_num-1]%1000) ;
    for(i = p_num-2 ; i >= 0 ; i--)
        printf("%c", c[i]%1000) ;
    printf("\n") ;
    return 0 ;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved