个人心得:被这周的专题名坑了,一直用字典树,明明题目看得很清楚了,不存在相同的字母,即每个字母最多只有一个直接后驱,那么只要用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

Input
3
bcd
ab
cdef
Output
abcdef
Input
4
x
y
z
w
Output
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 ;
}

最新文章

  1. jquery easy ui 1.3.4 布局layout(4)
  2. 数据库索引&lt;二&gt; 补充前篇 (上一篇抽风了,这个补上)
  3. mysql在一台服务器搭建主从
  4. WinFrom下连接字符串的数据库文件路径问题
  5. Git 经常使用的命令
  6. 创建UIButton
  7. scroll运用、图片悬浮
  8. Suse 创建NFS共享目录
  9. SAP HANA 能做什么
  10. 1988: Sn 爆long long 的处理方法
  11. iOS-状态栏字体颜色【白色】【Xcode9.1】
  12. 18 Loader代码案例
  13. CocosCreator检测动作执行完毕的方法~之一吧,应该= =
  14. 【vue】清理代码
  15. AIOps背景/所应具备技术能力分析(上)
  16. git创建新的分支
  17. Python并发编程一(多进程)
  18. 尝试笔记 01 之 CSS 边角上的标签
  19. iOS中UIView翻转效果实现
  20. visio 的使用方法

热门文章

  1. Struts2框架学习第三章——Struts2基础
  2. iOS 和Android客户端测试区别整理ing
  3. JQuery对象与javascript对象的转换
  4. Python写入CSV文件的问题
  5. C++:后缀表达式
  6. DB2 设置最大连接数
  7. Linq的使用 &lt;一&gt;
  8. Python之Fabric
  9. uname命令行
  10. Volatility2.4以上版本及fmem使用指南