• 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

    Input
    3
    bcd
    ab
    cdef
    Output
    abcdef
    Input
    4
    x
    y
    z
    w
    Output
    xyzw
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<set>
     5 #include<vector>
     6 #include<cstring>
     7 #include<iomanip>
     8 #include<algorithm>
     9 using namespace std;
    10 #define inf 1<<29
    11 #define nu 4000005
    12 #define maxnum 100005
    13 #define num 28
    14 int n;
    15 vector<int>G[num];
    16 int visit[num];
    17 void dfs(int i)
    18 {
    19     visit[i]=3;
    20     cout<<(char)(i+'a');
    21     for(int j=0;j<G[i].size();j++)
    22     {
    23         int t=G[i][j];
    24         if(visit[t]!=3){
    25             dfs(t);
    26         }
    27     }
    28 }
    29 int main()
    30 {
    31      char str[25];
    32     int i,n,j=0;
    33     scanf("%d",&n);
    34     getchar();
    35     memset(visit,0,sizeof(visit));
    36     while(n--){
    37     gets(str);
    38     int len=strlen(str);
    39     for(i=0;i<len-1;i++)
    40     {
    41         G[str[i]-'a'].push_back(str[i+1]-'a');
    42         visit[str[i+1]-'a']=2;
    43     }
    44     if(visit[str[0]-'a']!=2) visit[str[0]-'a']=1;
    45     }
    46     for(int i=0;i<num;i++){
    47         if(visit[i]==0) continue;
    48         if(visit[i]==1){
    49             dfs(i);
    50         }
    51     }
    52     return 0;
    53 }


  • 相关阅读:
    IE8下提示'console'未定义错误
    左右添加和删除
    箭头函数
    事件冒泡
    选中状态改变,并且实现左边选中便便添加
    appcan里面模板的使用
    白面机器学习笔记(一)
    常见的模型加速方法
    相机的参数
    深度学习和机器学习的区别
  • 原文地址:https://www.cnblogs.com/blvt/p/7931786.html
Copyright © 2020-2023  润新知