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,则把它加入 ...
随机推荐
- 7个常见Javascript框架介绍
设计开发中的“框架”指一套包含工具.函数库.约定,以及尝试从常用任务中抽象出可以复用的通用模块,目标是使设计师和开发人员把重点放在任务项目所特有的方面,避免重复开发.通俗的讲,框架就是最常用的java ...
- duilib入门简明教程 -- 界面设计器 DuiDesigner (10)
上一个教程讲解了怎么布局最大化.最小化.关闭按钮,但是如果手动去计算这三个按钮的位置和大小的话,非常的不直观,也很不方便. 所以这一章准备介绍duilib的UI设计器,由于这个设计器很不 ...
- 什么是C#?什么是.NET Framework?
1.什么是C#: 解1:C#就是一门开发语言,是由C及C++演变而来的,有朋友戏称之为"C四个+",这里的"#"号,不读"井",而读做&qu ...
- DEV 之 有些控件不允许拖动。
DEV 之 有些控件不允许拖动. 设置一个参数即可解决问题
- SQL SERVER先判断视图是否存在然后再创建视图的语句
如果我们的语句为: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 IF NOT EXISTS(SELECT 1 FROM sys.views WHERE name='Report_I ...
- Linux--多用户登录服务器端口抓包
以root身份登录1.新建用户组用命令groupadd test2.添加用户useradd -d /home/test/bei_1 -s /bin/sh -g test -m bei_1此命令新建了一 ...
- Struts2框架action路径问题心得----》页面url请求怎么找action
Struts2 页面url请求怎么找action Struts2 页面url请求如何找action 1.我们使用最原始的方法去查找action,不同注解. struts.xml文件先配置 <!- ...
- MyBatis框架流程
Hibernate与Mybatis的本质区别和应用场景 Hibernate:标准的ORM框架,不需要写SQL语句,但是优化和修改SQL语句比较难. 应用于需求变化固定的中小型的项目,例如后台管理系统. ...
- [转](译)KVO的内部实现
转载自:http://www.cocoachina.com/applenews/devnews/2014/0107/7667.html 09年的一篇文章,比较深入地阐述了KVO的内部实现. K ...
- P2319 [HNOI2006]超级英雄 题解
[HNOI2006]超级英雄 题目描述 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目, ...