codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】
1 second
256 megabytes
standard input
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.
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.
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.
3
bcd
ab
cdef
abcdef
4
x
y
z
w
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 */
codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】的更多相关文章
- bfs+dfs乱搞+类似拓扑排序——cf1182D
代码不知道上了多少补丁..终于过了 用类似拓扑排序的办法收缩整棵树得到x,然后找到x直连的最远的和最近的点 只有这三个点可能是根,依次判一下即可 另外题解的第一种方法时找直径,然后判两端点+重心+所有 ...
- 2017-2018 ACM-ICPC NEERC B题Berland Army 拓扑排序+非常伤脑筋的要求
题目链接:http://codeforces.com/contest/883/problem/B There are n military men in the Berland army. Some ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- 【CF883B】Berland Army 拓扑排序
[CF883B]Berland Army 题意:给出n个点,m条有向边,有的点的点权已知,其余的未知,点权都在1-k中.先希望你确定出所有点的点权,满足: 对于所有边a->b,a的点权>b ...
- Codeforces Round #363 Fix a Tree(树 拓扑排序)
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cs ...
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...
- CodeForces - 645D Robot Rapping Results Report(拓扑排序)
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some a ...
- Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...
- codeforces 510 C Fox And Names【拓扑排序】
题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...
随机推荐
- dom4j学习总结(一)
dom4j学习总结(一) (一)创建Document的基本操作 /** * xml基本操作 */ public void BaseOperation(){ //创建一个document Doc ...
- 【译文】不是所有的 bug 都值得修复的
原文作者:KRISTINE PINEDO 译者:白乐航 欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为软件开发者,您只需要为客户编写和交付出色的产品和功能. 但您也知道软件开发并不总是那么容 ...
- 不可逆转(SDOI2010地精部落)
提供一个简单一点的思路,不需要滚动数组,也不需要一些奇奇怪怪的性质. 我们考虑设\(f[i]\)为\(1\)到\(i\)中有多少种波动数列. 我们可以注意到,波动数列分为先降后升和先升后降两种,但是我 ...
- 如何用Python做Web开发?——Django环境配置
用Python做Web开发,Django框架是个非常好的起点.如何从零开始,配置好Django开发环境呢?本文带你一步步无痛上手. 概念 最近有个词儿很流行,叫做“全栈”(full stack ...
- leetcode 23. 合并K个排序链表 JAVA
题目: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: ...
- 求解任意图的最小支配集(Minimun Dominating Set)
给定一个无向图G =(V,E),其中V表示图中顶点集合,E表示边的集合.G的最小控制顶点集合为V的一个子集S∈V:假设集合R表示V排除集合S后剩余顶点集合,即R∩S=∅,R∪S=V:则最小控制顶点集合 ...
- 【English】20190430
Network security网络安全[ˈnetwɜːrk] [sɪˈkjʊrəti] Teradata Generic Security Service 通用安全服务[dʒəˈnerɪk] [s ...
- “全栈2019”Java第七十章:静态内部类详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- [ActionScript 3.0] 使TextField呈现手型的简单方法
有的人不知道这个方法,有时候为了给文本添加手型做了很多复杂的工作,虽然是可以实现,但心里有没有一种难以接受的感觉?那我们看看这个吧, 例子一看就懂: var str:String="显示手形 ...
- JAVA JVM 杂谈(一)
JVM能够跨计算机体系结构来执行Java字节码,主要是由于JVM屏蔽了与各个计算机平台先关的软件或者硬件之间的差异,使得与平台先关的耦合统一由JVM的提供者来实现. JVM结构组成: 1.类加载器:在 ...