codeforces-508d Tanya and Password (Oraton Road)

Source: Internet
Author: User

Description

While Dad is at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password isAstringConsisting of n +2Characters. She has written all the possible n three-letter continuous substrings of the password on pieces of paper, one forEach piece of paper, and threw the password out. Each three-letter substring is written the number of times it occurredinchThe password. Thus, Tanya ended up with n pieces of paper. Then Tanya realized that Dad would be upset to learn on her game and decided to restore the password or at least any stringcorresponding to the finalSetof Three-letter strings. You have theinch  ThisDifficult 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•  ), the number of three-lettersubstrings Tanya got. Next n lines contain three letters each, forming the substring of dad's password. Each character in the input was a lowercase or uppercase Latin letter or a digit.

Output

set of substrings Don't exist, print "NO".  isstringset'YES', and then print any suitable Password option.

Sample Input

Input
5 Acaabaabacabbac

Output
Yesabacaba

Input
4 Abcbcbcb1b13

Output
NO

Input
7 aaaaaaaaaaaaaaaaaaaaa

Output
Yesaaaaaaaaa

Test instructions: Give a n, enter a string of n length 3, the characters contain the case and number in English, and determine if there is a string of length n+2 containing all n substrings

Idea: PROBLEM Analysis: Can transform the question to judge whether there is Oraton road, how to compose? Because the title says that each string has a length of 3, so we take its first two characters as a node, the last two characters as a node, and then construct a forward graph, just to determine if there is a Euler path to the graph, Oraton Road is a path that passes through all the edges of a graph (no map or a map) once and only once across all vertices in the graph. This question and poj2377 a bit similar, but this problem has two points than that problem, 1 is the storage problem of the node, the title is the same as the end of the first character can be linked, and this problem needs to hash the node, because there are 10+26+26=62 characters, we first 62 characters from 1 to 61 number and then set the node to 62x+ Y integer variable, you can represent all the nodes. There is a problem is the amount of data, the subject of n is the number of sides to 20w, if you have already judged the existence of Oraton road, in accordance with the point of Dfs to find, parallel and self-ring a lot more likely to time out or even burst the stack, so we can use the edge to find, with the advantage of looking for the process can be Here's how to tell if there's a Euler pathway.

The degree of access is equal to the European pull circuit, DFS start any, if the difference between the degree of 1 points of the number is not greater than 2 will be large as the starting point, if the number is greater than 2 or the difference between the degree of more than 1 does not exist Euler channel

Hash each point first

And then determine the starting point and the end point, the beginning is the degree

The end point is the location of the freshman degree.

And then we'll just have to start the DFS.

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 300000 at #defineINF 1e12 - intN; - stringS,ans; -vector<int>Edge[n]; - int inch[N], out[n],cnt[n]; - voidDfsinti) { in     while(cnt[i]<edge[i].size ()) { -DFS (edge[i][cnt[i]++]); to    } +ans+= (Char) i% the; - } the  * intMain () $ {Panax Notoginseng     while(SCANF ("%d", &n) = =1){ -       intu,v; the        for(intI=0; i<n;i++){ +Cin>>s; Au=s[0]* the+s[1]; thev=s[1]* the+s[2]; + Edge[u].push_back (v); -          inch[v]++; $           out[u]++; $       } -       intstart=u; -       intL=0, r=0; the       intflag=1; -        for(intI=0; i<n;i++){Wuyi          intD=inch[i]- out[i]; the          if(d==-1){ -l++; Wustart=i; -}Else if(d==1){ Aboutr++; $}Else if(d!=0){ -printf"no\n"); -flag=0; -              Break; A          } +          if(l>1|| R>1){ theprintf"no\n"); -flag=0; $              Break; the          } the       } the       if(flag==0){ the          Continue; -       } in dfs (start); theans+= (Char) (start/ the); the Reverse (Ans.begin (), Ans.end ()); About       if(Ans.length ()!=n+2){ theprintf"no\n"); the}Else{ theprintf"yes\n"); +cout<<ans<<Endl; -       } the    }Bayi     return 0; the}
View Code

codeforces-508d Tanya and Password (Oraton Road)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.