Making genome in Berlandtime limit per test1 secondmemory limit per test256 Megabytesinputstandard Inputoutputstandard out Put
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 so we ve used to:it can have a distinct NUCL Eotide types, a nucleotide of each type can occur at the most once. If we assign distinct 中文版 letters to all nucleotides and then the genome of a berland dinosaur would represent a non-empty String consisting of small 中文版 letters, such that is occurs in it for most once.
Scientists has n genome fragments that is 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, the input is not contradictory, and at least one suitable line always exists. When the scientists found out is a strong programmer, they asked you addition to choose the one with the mini Mum length. If There is 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 f Ragments.
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 Chinese alphabet. It isn't guaranteed that the given fragments is distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one.
It is guaranteed this there is such string of distinct letters, contains all the given fragments as substrings.
Output
The of the single line of the output print the genome of the minimum length is contains all the given parts. All the nucleotides in the genome must is distinct. If there is multiple suitable strings, print the string of the minimum length. If There also is multiple suitable strings, you can print any of the them.
Examplesinput
3
Bcd
Ab
Cdef
Output
ABCdef
Input
4
X
Y
Z
W
Output
Xyzw
The main topic: give you n substring, each character in the substring is different, ask you to find the shortest original string, of course, the characters in the original string are also different. Only 26 lowercase letters.
How to solve the problem: we need to understand that there is either a unique letter behind each letter, or not. So as long as the first letter of one of my substrings does not appear in the non-first character of the other substring, then I can use this substring as a non-precursor node. such as: Abc,ab,efg,fgk. Because a does not appear as a non-first character in other substrings, a can be a non-precursor node, and e can also. So all we have to do is record the characters behind each character, and then just hand it back to find it.
#include <stdio.h> #include <algorithm> #include <string.h> #include <math.h> #include < string> #include <iostream> #include <queue> #include <stack> #include <limits.h> #include <map> #include <vector> #include <set>using namespace std;typedef long long ll;typedef unsigned long Long ULL; #define MID (L+r)/2#define lson rt*2,l,mid#define rson rt*2+1,mid+1,rconst int mod = 1e9+7;const int maxn = 1e2+2 00;//const LL inf = 0x3f3f3f3f3f3f3f3f;const int inf = 0x3f3f3f3f;int vis[30];string s;int ind[maxn];vector<int>g[m axn];void dfs (int u) {vis[u] = 2; s + = U + ' a '; for (int i = 0; i < g[u].size (); i++) {int& v = g[u][i]; if (vis[v]! = 2) {dfs (v); }}}int Main () {int n; while (scanf ("%d", &n)!=eof) {for (int i = 1; I <= n; i++) {cin>>s; for (int j = 0; J < s.size ()-1; j + +) {g[s[j]-' a '].push_back (s[j+1]-' a '); Vis[s[j+1]-' a '] = 3; } if (vis[s[0]-' a ']! = 3) {vis[s[0]-' a '] = 1; }} s = ""; for (int i = 0; i < i++) {if (vis[i] = = 1) {DFS (i); }} cout<<s<<endl; } return 0;} /*4ababababc*/
Codeforces 638b--making genome in Berland —————— "Similar topology sort"