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,则把它加入 ...
随机推荐
- windows8.1 windows defender service无法启动解决方案
分析:当pc安装第三方杀毒软件,比如360杀毒,这时候360会强制关闭mse杀毒服务,无法在服务栏目里启动.由于windows defender service是系统强制启动进程,无法在系统启动后进行 ...
- thinkphp里数据嵌套循环
做thinkphp时要用到循环里面嵌套循环的,并第二个循环是和外面的有关联的. thinkphp官网给出的文档为: <volist name="list" id=" ...
- solidity 合约间调用以及参数传递
在 以太坊中合约间是可以相互调用,并且正常进行参数传递以及返回值处理. contract1.sol pragma solidity ^0.4.0; contract Test1 { uint256 p ...
- “全栈2019”Java第八十二章:嵌套接口能否访问外部类中的成员?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Java面向对象之异常(throw与throws)
一.基础概念 1.throw和throws的区别: 位置不同:throws用在函数上,后面跟的是异常类,可以跟多个. throw用在函数内,后面跟的是异常对象. 功能不同:throws用来声明异常,让 ...
- redis 3.0 集群__故障测评
一, slave 是不能通过redis-cli 直接进行读写操作的,但是可以执行 keys, info 命令( 猜测类似全局性的不影响到原子性操作的命令应该都可以,没有一一试验) 二,集群中的某节点异 ...
- uC/OS-II 函数之OSInit()
获得更多资料欢迎进入我的网站或者 csdn或者博客园 对于有热心的小伙伴在微博上私信我,说我的uC/OS-II 一些函数简介篇幅有些过于长应该分开介绍.应小伙伴的要求,特此将文章分开进行讲解.本文主要 ...
- 安装php,nginx 带debug
gdb安装包 在CentOS6.4下使用gdb进行调试的时候, 使用bt(breaktrace)命令时,会弹出如下的提示: 头一天提示: Missing separate debuginfos, ...
- 获取指定<文字行数>的<高度>是多少 TextKit
- (CGSize)maxLineSizeWithLines:(NSInteger)lines constraintSize:(CGSize)size attributes:(NSDictionary ...
- DEM反应添加顺序注意问题
在含有DEM反应的dat中,均相反应的block要在DEM反应之前,例如: @(RXNS) (some reaction equations) @(END) @(DES_RXNS) (some rea ...