Making Genome in Berland (DFS+思维)
个人心得:被这周的专题名坑了,一直用字典树,明明题目看得很清楚了,不存在相同的字母,即每个字母最多只有一个直接后驱,那么只要用DFS走开头就好了,
思想很巧妙,用vector,记录后驱,同时用visit确定是否访问和化简后的字符串谁是第一个开头,visit的值1表示单独存在的头,2表示是否访问,3是用来记录确定是否是单独存在的。
服气服气,以后思路要开阔些,这样才能达到训练和学习的目的。
题目:
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.
Example
3
bcd
ab
cdef
abcdef
4
x
y
z
w
xyzw
#include<iostream>
#include<cstdio>
#include<cmath>
#include<set>
#include<vector>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 4000005
#define maxnum 100005
#define num 28
int n;
vector<int>G[num];
int visit[num];
void dfs(int i)
{
visit[i]=;
cout<<(char)(i+'a');
for(int j=;j<G[i].size();j++)
{
int t=G[i][j];
if(visit[t]!=){
dfs(t);
}
}
}
int main()
{
char str[];
int i,n,j=;
scanf("%d",&n);
getchar();
memset(visit,,sizeof(visit));
while(n--){
gets(str);
int len=strlen(str);
for(i=;i<len-;i++)
{
G[str[i]-'a'].push_back(str[i+]-'a');
visit[str[i+]-'a']=;
}
if(visit[str[]-'a']!=) visit[str[]-'a']=;
}
for(int i=;i<num;i++){
if(visit[i]==) continue;
if(visit[i]==){
dfs(i);
}
}
return ;
}
Making Genome in Berland (DFS+思维)的更多相关文章
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题
B. Making Genome in Berland 题目连接: http://www.codeforces.com/contest/638/problem/B Description Berlan ...
- codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】
Making Genome in Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...
- hdu6035[dfs+思维] 2017多校1
/*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...
- D. Eternal Victory(dfs + 思维)
D. Eternal Victory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hiboCoder 1041 国庆出游 dfs+思维
先抽象出一棵以1做为根结点的树.给定了访问序列a[1..m]. 考虑两种特殊情况: 1.访问了某个a[j],但是存在a[i]没有访问且i < j,出现这种情况说明a[j]一定是a[i]的祖先节点 ...
- newcoder F石头剪刀布(DFS + 思维)题解
题意:wzms 今年举办了一场剪刀石头布大赛,bleaves 被选为负责人. 比赛共有 2n 个人参加, 分为 n 轮, 在每轮中,第 1 位选手和第 2 位选手对战,胜者作为新的第 1 位选手, 第 ...
- Codeforces 931D Peculiar apple-tree(dfs+思维)
题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...
随机推荐
- MongoDB 查看所有用户账号信息
在 MongoDB 中创建了很多帐号,怎么查看所有帐号信息? 1. 查看全局所有账户 2. 查看当前库下的账户 查看全局所有账户 : > use admin switched to db adm ...
- 15 个有趣的 JS 和 CSS 库
开发者们,一起来看看有木有你需要的前端库. 1. DisplayJS DisplayJS 是一个帮助你渲染 DOM 的简易框架.使用它,你可以更容易地将 JS 变量遍历到特定的 HTML 元素中,类似 ...
- flask学习(十):模板中访问模型和字典的属性
访问模型中的属性或者是字典,可以通过{{params.property}}的形式,或者是使用{{params['age']}}这样的形式
- 搞懂分布式技术10:LVS实现负载均衡的原理与实践
搞懂分布式技术10:LVS实现负载均衡的原理与实践 浅析负载均衡及LVS实现 原创: fireflyc 写程序的康德 2017-09-19 负载均衡 负载均衡(Load Balance,缩写LB)是一 ...
- 常用git命令(一)
git add 命令. 这是个多功能命令:可以用它开始跟踪新文件,或者把已跟踪的文件放到暂存区,还能用于合并时把有冲突的文件标记为已解决状态等. 将这个命令理解为“添加内容到下一次提交中”而不是“将一 ...
- Mybatis Generator 扩展
目标 修改Model的名称 修改Dao的名称 配置文件 context.targetRuntime 替换为自定义的类型 原理在:org.mybatis.generator.internal.Objec ...
- day29 主机管理-堡垒机2-原生ssh会话记录
day29课堂代码:https://github.com/liyongsan/git_class/tree/master/day29 课堂笔记: 通过原生Ssh 记录会话1. 在我们自己的堡垒机交互脚 ...
- ( 转)Sqlserver中tinyint, smallint, int, bigint的区别 及 10进制转换16进制的方法
一.类型比较 bigint:从-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型数据,存储大小为 8 个字节.一个字节就是8位,那么b ...
- js 验证对象是否为数组
一.方法一:用到了原型 由于typeof检测数组,只会显示其为object,并不会详细到告诉我们是否为array,所以我们可以自己写个js用原型来检测: <script> /** * * ...
- Django中构造响应对象的方式
1 HttpResponse 可以使用django.http.HttpResponse来构造响应对象. HttpResponse(content=响应体, content_type=响应体数据类型, ...