VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题
B. Making Genome in Berland
题目连接:
http://www.codeforces.com/contest/638/problem/B
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
3
bcd
ab
cdef
Sample Output
abcdef
Hint
题意
有一个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;
}
VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题的更多相关文章
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland
今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...
- VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力
D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...
- VK Cup 2016 - Qualification Round 2 C. Road Improvement dfs
C. Road Improvement 题目连接: http://www.codeforces.com/contest/638/problem/C Description In Berland the ...
- VK Cup 2016 - Qualification Round 2 A. Home Numbers 水题
A. Home Numbers 题目连接: http://www.codeforces.com/contest/638/problem/A Description The main street of ...
- VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) D. Running with Obstacles 贪心
D. Running with Obstacles 题目连接: http://www.codeforces.com/contest/637/problem/D Description A sports ...
- VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) C. Promocodes with Mistakes 水题
C. Promocodes with Mistakes 题目连接: http://www.codeforces.com/contest/637/problem/C Description During ...
- VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) B. Chat Order 水题
B. Chat Order 题目连接: http://www.codeforces.com/contest/637/problem/B Description Polycarp is a big lo ...
- VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) A. Voting for Photos 水题
A. Voting for Photos 题目连接: http://www.codeforces.com/contest/637/problem/A Description After celebra ...
- VK Cup 2016 - Qualification Round 1——A. Voting for Photos(queue+map)
A. Voting for Photos time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- C++ 内联函数inline
http://blog.csdn.net/u011327981/article/details/50601800 1. 内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: 复制代码 代码 ...
- Tutorial 3: Class-based Views
转载自:http://www.django-rest-framework.org/tutorial/3-class-based-views/ Tutorial 3: Class-based Views ...
- “您查看的网页正在试图关闭窗口。是否关闭此窗口”的屏蔽方法(JavaScript)
原文:http://www.cnblogs.com/tigerhuolh/archive/2011/04/14/2015634.html 用JS代码关闭窗口时会提示“您查看的网页正在试图关闭窗口.是否 ...
- C语言花括号
由于C语言本身就是函数式语言,说白了,C程序就是由函数构成的! 所以花括号肯定是用在函数之中,包括函数中的各种流程控制语句中. 实际上,C程序中花括号{}的作用:就是把多个单条语句用花括号{}括起来组 ...
- cvc-complex-type.2.4.a: Invalid content was found starting with element ‘init-param’(转)
在写xml的时候又一次总是报cvc-complex-type.2.4.a: Invalid content was found starting with element 错误,还出现小红叉,在网上找 ...
- socket-----爬虫&&文件传输
最近想着写几个小demo 写了一个爬虫,用的是C++,基本思想就是一层一层的找类似深搜吧,抓取的页面是www.cnblogs.com,从localhost发送request请求,给www.cnblog ...
- Django之进阶相关操作
一.QuerySet的特点 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. 1 >>> Entry.objec ...
- 几个python one-liner
生成斐波那契数列的前10个数,从1开始.若生成前n个,改为range(n-2).代码很简单: List = reduce(lambda x, y: x + [x[-1] + x[-2]], range ...
- C++函数的重载、覆盖和隐藏区别
a.成员函数被重载的特征:(1)相同的范围(在同一个类中)(2)函数名字相同(3)参数不同(4)virtual 关键字可有可无b.覆盖是指派生类函数覆盖基类函数,特征是:(1)不同的范围(分别位于派生 ...
- 使用CSS更改图标的颜色
我们经常在很多网站上见到更改网站的主题时,图标的颜色也改变了,我们总是觉的这一项功能非常伟大,因为我们知道使用CSS是无法完成更改图片的颜色的.那么,网站上随心所欲的图标颜色是采用N多个图片不断的切换 ...