1. 程式人生 > >Making Genome in Berland (DFS+思維)

Making Genome in Berland (DFS+思維)

put win types addition ecif pes set mini cati

個人心得:被這周的專題名坑了,一直用字典樹,明明題目看得很清楚了,不存在相同的字母,即每個字母最多只有一個直接後驅,那麽只要用DFS走開頭就好了,

思想很巧妙,用vector,記錄後驅,同時用visit確定是否訪問和化簡後的字符串誰是第一個開頭,visit的值1表示單獨存在的頭,2表示是否訪問,3是用來記錄確定是否是單獨存在的。

服氣服氣,以後思路要開闊些,這樣才能達到訓練和學習的目的。

題目:

Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we‘ve used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct English letters to all nucleotides, then the genome of a Berland dinosaur will represent a non-empty string consisting of small English letters, such that each letter occurs in it at most once.

Scientists have n genome fragments that are represented as substrings (non-empty sequences of consecutive nucleotides) of the sought genome.

You face the following problem: help scientists restore the dinosaur genome. It is guaranteed that the input is not contradictory and at least one suitable line always exists. When the scientists found out that you are a strong programmer, they asked you in addition to choose the one with the minimum length. If there are multiple such strings, choose any string.

Input

The first line of the input contains a positive integer n (1?≤?n?≤?100) — the number of genome fragments.

Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one.

It is guaranteed that there is such string of distinct letters that contains all the given fragments as substrings.

Output

In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them.

Example

Input
3
bcd
ab
cdef
Output
abcdef
Input
4
x
y
z
w
Output
xyzw
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<set>
 5 #include<vector>
 6 #include<cstring>
 7 #include<iomanip>
 8 #include<algorithm>
 9 using namespace std;
10 #define inf 1<<29
11 #define nu 4000005
12 #define maxnum 100005
13 #define num 28
14 int n;
15 vector<int>G[num];
16 int visit[num];
17 void dfs(int i)
18 {
19     visit[i]=3;
20     cout<<(char)(i+a);
21     for(int j=0;j<G[i].size();j++)
22     {
23         int t=G[i][j];
24         if(visit[t]!=3){
25             dfs(t);
26         }
27     }
28 }
29 int main()
30 {
31      char str[25];
32     int i,n,j=0;
33     scanf("%d",&n);
34     getchar();
35     memset(visit,0,sizeof(visit));
36     while(n--){
37     gets(str);
38     int len=strlen(str);
39     for(i=0;i<len-1;i++)
40     {
41         G[str[i]-a].push_back(str[i+1]-a);
42         visit[str[i+1]-a]=2;
43     }
44     if(visit[str[0]-a]!=2) visit[str[0]-a]=1;
45     }
46     for(int i=0;i<num;i++){
47         if(visit[i]==0) continue;
48         if(visit[i]==1){
49             dfs(i);
50         }
51     }
52     return 0;
53 }



Making Genome in Berland (DFS+思維)