VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题
2016-03-25 12:44
363 查看
B. Making Genome in Berland
题目连接:
http://www.codeforces.com/contest/638/problem/BDescription
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
3bcd
ab
cdef
Sample Output
abcdefHint
题意
有一个dna片段,这个片段只含有26个英文字母,且这些字母最多出现一次现在我们截取了n个片段,现在想让你还原出原来的dna片段是啥
请还原出最短的那个,保证有解
题解:
当成图论题来做就好了。由于保证是截取的n个片段,所以直接dfs维护一个拓扑序列就好了
然后就完了……
代码
#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; }
相关文章推荐
- centos 6.7 perl 5.22 安装DBD 需要使用老的perl版本
- centos 6.7 perl 5.22 安装DBD 需要使用老的perl版本
- RGBD物体识别(4)--使用opencv3读取kinect数据
- java中最小生成树的实现
- maven 笔记
- 詩經神學國際論文1:日能習財武經
- C++作业2
- 移动联通电信运营商手机号段分配
- leetcode 234: Palindrome Linked List
- JQuery判断页面是否按下了Enter键
- Win7下使用Telnet命令
- Unity发布时出现“The type or namespace name `UnityEditor' could not be found.” 错误
- SQL语句中 and or执行优先级
- hdu 1241 Oil Deposits (BFS)
- 图片压缩总结
- 关于Eclipse自动补全的问题
- Ubuntu 使用中遇到的小问题(持续更新)
- 【转】KVM/Installation
- 客户端发一个post请求
- 深拷贝的现代写法