codeforces 638B—— Making Genome in Berland——————类似拓扑排序
Posted tc_goshawk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 638B—— Making Genome in Berland——————类似拓扑排序相关的知识,希望对你有一定的参考价值。
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.
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.
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.
3
bcd
ab
cdef
abcdef
4
x
y
z
w
xyzw
题目大意:给你n个子串,子串中每个字符都不同,问你找到最短的原串,当然原串中字符也都不同。只有26个小写字母。
解题思路:我们需要明白,每个字母后边要么有唯一确定的字母,要么没有。那么只要我的某个子串的第一个字母不在其他子串的非第一个字符中出现,那么我这个子串就可以作为一个无前驱的结点。如:abc,ab,efg,fgk。由于a不在其他子串的非第一个字符出现,所以a可以作为一个无前驱的结点,e也可以。所以我们只要记录每个字符后边的字符,然后直接递归去找即可。
#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,R const int mod = 1e9+7; const int maxn = 1e2+200; //const LL INF = 0x3f3f3f3f3f3f3f3f; const int INF = 0x3f3f3f3f; int vis[30]; string s; int ind[maxn]; vector<int>G[maxn]; 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 < 26; i++){ if(vis[i] == 1){ dfs(i); } } cout<<s<<endl; } return 0; } /* 4 ab ab ab abc */
以上是关于codeforces 638B—— Making Genome in Berland——————类似拓扑排序的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #379 (Div. 2) C. Anton and Making Potions(二分)
Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)
Codeforces 1229B Kamil and Making a Stream
Codeforces 734C Anton and Making Potions(枚举+二分)