CodeForces 638B Making Genome in Berland
2016-04-19 21:34
309 查看
思路:dfs维护一个拓扑序就好了
Description
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.
Sample Input
Input
Output
Input
Output
#include<bits/stdc++.h> using namespace std; const int maxn = 250; vector<int>e[30]; int vis[maxn]; string ans; void dfs(int x) { vis[x]=2; for (int i = 0;i<e[x].size();i++) { if (vis[e[x][i]]==2) continue; dfs(e[x][i]); } ans+=(char)('a'+x); } int main() { int n; scanf("%d",&n); for (int i = 1;i<=n;i++) { string s; cin >> s; for (int j = 0;j<s.size()-1;j++) e[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; } for (int i = 0;i<26;i++) if (vis[i]==1) dfs(i); reverse(ans.begin(),ans.end()); cout << ans << endl; }
Description
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.
Sample Input
Input
3 bcd ab cdef
Output
abcdef
Input
4 x y z w
Output
xyzw
相关文章推荐
- PHP mail() 函数
- swift快捷键大全
- 充分利用时间 踏踏实实
- 典型用户分析
- pda完美结合开销售单据haohantech-安卓打票扫描PDA智能POS 发展现状如何,会取代传统电脑 代替手工开单 么?
- 车站订票系统可行性分析报告
- linux快捷键
- executeScalar ExecuteNonQuery
- android将对象写入文件和从文件中读取对象数据
- Python趣味编程---Python也会讲笑话
- 密码发生器
- 有效数字的保留
- Qt中的窗口部件
- 【实践】获取CKEditor的html文本、纯文本、被选中的内容及赋值
- JS断点调试心得(转)
- 阶乘因式分解(—)
- spark源码分析之Executor启动与任务提交篇
- Android源码国内源
- 放棋子
- DoDataExchange DDX_Control 与 DDX_Text 区别