VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题

时间:2022-12-13 19:16:07

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 水题的更多相关文章

  1. VK Cup 2016 - Qualification Round 2 B&period; Making Genome in Berland

    今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...

  2. VK Cup 2016 - Qualification Round 2 D&period; Three-dimensional Turtle Super Computer 暴力

    D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...

  3. VK Cup 2016 - Qualification Round 2 C&period; Road Improvement dfs

    C. Road Improvement 题目连接: http://www.codeforces.com/contest/638/problem/C Description In Berland the ...

  4. VK Cup 2016 - Qualification Round 2 A&period; Home Numbers 水题

    A. Home Numbers 题目连接: http://www.codeforces.com/contest/638/problem/A Description The main street of ...

  5. VK Cup 2016 - Qualification Round 1 &lpar;Russian-Speaking Only&comma; for VK Cup teams&rpar; D&period; Running with Obstacles 贪心

    D. Running with Obstacles 题目连接: http://www.codeforces.com/contest/637/problem/D Description A sports ...

  6. VK Cup 2016 - Qualification Round 1 &lpar;Russian-Speaking Only&comma; for VK Cup teams&rpar; C&period; Promocodes with Mistakes 水题

    C. Promocodes with Mistakes 题目连接: http://www.codeforces.com/contest/637/problem/C Description During ...

  7. VK Cup 2016 - Qualification Round 1 &lpar;Russian-Speaking Only&comma; for VK Cup teams&rpar; B&period; Chat Order 水题

    B. Chat Order 题目连接: http://www.codeforces.com/contest/637/problem/B Description Polycarp is a big lo ...

  8. VK Cup 2016 - Qualification Round 1 &lpar;Russian-Speaking Only&comma; for VK Cup teams&rpar; A&period; Voting for Photos 水题

    A. Voting for Photos 题目连接: http://www.codeforces.com/contest/637/problem/A Description After celebra ...

  9. VK Cup 2016 - Qualification Round 1——A&period; Voting for Photos(queue&plus;map)

    A. Voting for Photos time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. Net&period;Sf&period;Json java Object to JsonObject

    public class People{ private String name; public void setName(String name){ this.name = name; } publ ...

  2. linux中的数值运算

    一.declare 作用:声明变量类型,bash默认变量为字符串类型的,并且字符串在拼接时直接拼接,不需要加号 使用方法: 二.数值运算 加法运算 a= b= c=$(($a+$b)) echo $c

  3. UIProgressView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  4. pollard&lowbar;rho和Miller&lowbar;Rabin

    Miller_Rabin就是以概论大小来判断素数 可以判断2^63范围的数 pollard_rho推荐两个很好的博客来理解:整数分解费马方法以及Pollard rho和[ZZ]Pollard Rho算 ...

  5. iOS开发之指纹解锁

    http://blog.csdn.net/hongfengkt/article/details/49868073 前一阵子一直在赶项目进度,没有太多时间写博客,现在终于空闲了,将以前欠下的博客补上来. ...

  6. JS事件处理程序

    JS事件处理程序:HTML事件处理程序.DOM0级事件处理程序.DOM2级事件处理程序.IE事件处理程序.跨浏览器的事件处理程序. HTML事件处理程序 <script type="t ...

  7. position&comma;display&comma;float&comma;overflow&comma;margin&comma;padding之间的相互影响

    1.元素分为块级元素和行内元素, 块级元素可以设置宽高,会自动换行,并且会发生相邻margin的合并问题.行内元素设置宽和高无效,以水平方向排列,(行内元素,绝对定位,浮动元素不会发生外边距合并)并且 ...

  8. HDU 1495 非常可乐&lpar;数论,BFS&rpar;

    非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. python类型转换convert实例分析

    在python的开发过程中,难免会遇到类型转换,这里给出常见的类型转换demo: 类型 说明 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 ...

  10. logical&lowbar;backup&colon; expdp&sol;impdp

    Table of Contents 1. 注意事项 2. 前期准备 3. 常用参数及示例 4. 常用语句示例 5. 交互式命令 6. 技巧 6.1. 不生成文件直接导入目标数据库 6.2. 通过she ...