• poj 3080 Blue Jeans(kmp)


    Description

    The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. 

    As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers. 

    A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC. 

    Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.

    Input

    Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
    • A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
    • m lines each containing a single base sequence consisting of 60 bases.

    Output

    For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

    Sample Input

    3
    2
    GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    3
    GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
    GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
    GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
    3
    CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
    ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

    Sample Output

    no significant commonalities
    AGATAC
    CATCATCAT
    题意:先输入的测试数据的组数,接下来是每组数据的字符串数,每组字符串有60个字符,求最长公共子串,如果最长的公共子串长度小于3,就输出
    no significant commonalities,否则输出字典序最小的最长公共子串。
    思路:枚举第一个串的子串为模板与其他的字串比较。
    ac代码:
    
    
     1 #include<stdio.h>
     2  2 #include<string.h>
     3  3 char s1[200],s2[15][200];
     4  4 int next[200],cnt,n1,n2,n,ma;
     5  5 void nextshuzu()                                      //next数组
     6  6 {
     7  7     int i=0,j=-1;
     8  8     next[0]=-1;
     9  9     while(i<n1)
    10 10     {
    11 11         if(j==-1||s1[i]==s1[j])
    12 12             next[++i]=++j;
    13 13         else
    14 14             j=next[j];
    15 15     }
    16 16 }
    17 17 void kmp()
    18 18 {
    19 19     nextshuzu();
    20 20     int i=0,j=0;
    21 21     ma=100;
    22 22     for(int k=1; k<n; k++)
    23 23     {
    24 24         int m=0;
    25 25         while(j<n2&&i<60)
    26 26         {
    27 27             if(j==-1||s1[j]==s2[k][i])
    28 28             {
    29 29                 i++;
    30 30                 j++;
    31 31             }
    32 32             else
    33 33                 j=next[j];
    34 34             if(j>m)                                        //求出这个子串的最大值
    35 35                 m=j;
    36 36         }
    37 37         if(m<ma)                                           //取出最小的那个,最小的都满足
    38 38         ma=m;
    39 39     }
    40 40 }
    41 41 int main()
    42 42 {
    43 43     int b;
    44 44     char s[200];
    45 45     scanf("%d",&b);
    46 46     while(b--)
    47 47     {
    48 48         scanf("%d",&n);
    49 49         for(int i=0;i<n;i++)
    50 50         scanf("%s",s2[i]);
    51 51         int ans=0;
    52 52         for(int i=0;i<58;i++)
    53 53         {
    54 54             strcpy(s1,s2[0]+i);
    55 55             n2=60-i;
    56 56             kmp();
    57 57             if(ans<ma)
    58 58             {
    59 59                 ans=ma;
    60 60                 strncpy(s,s2[0]+i,ans);
    61 61                 s[ans]='';
    62 62             }
    63 63             else if(ma==ans)                                        //开始第一次没写这里,wr后看别人的博客加上的,首先谢谢那个大神,在这我就先用了啊
    64 64             {                                                       //如果长度相同,用字典序最小的那个
    65 65                 char s3[200];
    66 66                 strncpy(s3,s2[0]+i,ans);
    67 67                 s3[ans]='';
    68 68                 if(strcmp(s,s3)>0)
    69 69                 strcpy(s,s3);
    70 70             }
    71 71         }
    72 72         if(ans>2)
    73 73         printf("%s
    ",s);
    74 74         else
    75 75         printf("no significant commonalities
    ");
    76 76     }
    77 77 }
    View Code
  • 相关阅读:
    二叉树知识拓展
    【2014年鄞州区】小幸福(e.pas/c/cpp)
    【2017年常州市】小X与队列 (queue)
    【2018年南海区甲组】扑克游戏(poker)
    【2018年南海区甲组】拆除桥墩(remove)
    【NOIP普及组模拟赛3】投影(skyline)
    常用正则表达式
    asp.net访问母版页控件方法
    C#文件读写操作
    C#读取csv格式文件
  • 原文地址:https://www.cnblogs.com/Xacm/p/3982309.html
Copyright © 2020-2023  润新知