codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】

Making Genome in Berland

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

3bcdabcdef

output

abcdef

input

4xyzw

output

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

*/

  

				
时间: 2024-10-12 17:42:08

codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】的相关文章

【CF883B】Berland Army 拓扑排序

[CF883B]Berland Army 题意:给出n个点,m条有向边,有的点的点权已知,其余的未知,点权都在1-k中.先希望你确定出所有点的点权,满足: 对于所有边a->b,a的点权>b的点权对于i=1..k,至少有一个点的点权为i n,m,k<=100000 题解:像菜肴制作一样奇怪的拓扑排序题,直接上方法吧,不会证. 先正反跑两边拓扑排序,得出每个点点权的下界Li和上界Ri. 将所有点按上界从小到大排序,然后枚举权值i.将所有上界为i的点都扔到堆中,再从堆里取出下界最大的那个点,将

Codeforces Round #363 Fix a Tree(树 拓扑排序)

先做拓扑排序,再bfs处理 #include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<map>#include<stack>#include<queue>#include<vector>#include<cmath&g

【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环

[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一个简单环,则欲删除的边一定经过该环.尝试环上的每一条边(至多n条边)后再次拓扑排序判断全图是否有环. 拓扑排序后定位到简单环:剩余图是环+环内DAG,DFS过程中将走入死路的点标-1,访问过标1,找到访问过的点就是简单环.换起始点直到找到环为止. 复杂度O(nm). #include<cstdio>

codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)

题目链接: D. Robot Rapping Results Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alt

codeforces 510 C Fox And Names【拓扑排序】

题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入队列中,并将它指向的节点的入度减去1 另外如果len[i]<len[i-1],说明第i个字符串是i-1个字符串的前缀,但是它还排在后面,这样肯定不存在符合的字典序,直接输出“Impossible” 1 #include<iostream> 2 #include<cstdio> 3

hdu 5438(类似拓扑排序)

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3178    Accepted Submission(s): 988 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, a

Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)

D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil created a following problem about putting 1?×?2 tiles into an n?×?m grid: "There is a grid with some cells that a

CSU 1804: 有向无环图 拓扑排序 图论

1804: 有向无环图 Submit Page   Summary   Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 716     Solved: 298 Description Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,-,n 编号. 设 count(x,y) 表示点 x 到点 y 不同的路径数量(规定 count(x,x)=0

贪心+拓扑排序 AOJ 2456 Usoperanto

题目传送门 题意:给出一条链,比如x连到y,x一定要在y的左边,且代价是这条链经过的点的权值和,问如何排序使得代价最小 分析:类似拓扑排序,先把入度为0的点入队,把指向该点的所有点按照权值排序,保证这样是代价是最小的,然后把这一块看成一个点继续入队.看图更简单: /************************************************ * Author :Running_Time * Created Time :2015/10/3 星期六 13:02:41 * File