zoukankan      html  css  js  c++  java
  • KMP专场 POJ

    I - kmp
    Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
    Submit

    Status

    Practice

    POJ 3080
    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

    /*********************************
         author   : Grant Yuan
         algorithm; kmp
         source   : POJ 3080
    	  time    ;2014/10/3 20:38
     *********************************/   
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cstdio>
    
    using namespace std;
    char a[12][62];
    char arr[62];
    int ans;
    char ansarr[62];
    int n,next[62],l1,l2;
    void get_next()
    {
        int i=0,j=-1;
        next[0]=-1;
        while(i<l1){
            if(arr[i]==arr[j]||j==-1){
                i++;j++;
                next[i]=j;
            }
            else j=next[j];
        }
    }
    
    bool kmp(char *s)
    {
        int sum=0;
        int i=0,j=0;
        while(i<l2){
            if(arr[j]==s[i]) {i++;j++;}
            else{j=next[j];if(j==-1){i++;j=0;}}
            if(j==l1) sum++;
        }
        if(sum>0) return true;
        return false;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
          scanf("%d",&n);
          memset(arr,0,sizeof(arr));
          ans=0;
          for(int i=0;i<n;i++)
          {
    
              scanf("%s",a[i]);
          }
           for(int i=0;i<=59;i++)
               for(int j=1;j+i<=60;j++)
           {  
               memset(arr,0,sizeof(arr));
               strncpy(arr,a[0]+i,j);
               get_next();
               l1=strlen(arr);l2=60;
               bool flag=1;
               for(int k=1;k<n;k++)
               {
                   if(kmp(a[k])==0) {flag=0;break;}
               }
               if(flag) {
                    if(l1>ans){ans=l1;memset(ansarr,0,sizeof(ansarr));strcpy(ansarr,arr);}
                    else if(l1==ans){ans=l1;if(strcmp(arr,ansarr)<0) strcpy(ansarr,arr);}
               }
          }
        if(ans<3) printf("no significant commonalities
    ");
          else printf("%s
    ",ansarr);}
        return 0;
    }
    


     

  • 相关阅读:
    GO语言网络编程
    GO语言测试
    GO语言反射
    GO语言strconv包的使用
    GO语言并发
    Centos7 开启swap分区
    设计模式 之 命令模式
    设计模式 之 代理模式
    设计模式 之 工厂模式
    设计模式 之 观察者模式
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254420.html
Copyright © 2011-2022 走看看