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;
    }
    


     

  • 相关阅读:
    java 实现前面带零的数字进行加减运算(保留前面的0)
    java 判断字符串是否是整数(纯数字:正整数、负整数、0)、至少包含一位小数、数字(可以是整数或小数)
    java 抽象类使用@Autowrited注入对象,子类直接使用父类的该属性
    java、springboot使用proguard混淆代码
    idea 使用阿里云仓库下载的jar包出现证书校验问题(PKIX:unable to find valid certification path to requested target)
    windows10安装zookeeper-3.6.2并生成zookeeper服务
    zookeeper-3.4.8 集群搭建
    zookeeper安装和使用 windows环境
    Dubbo入门---搭建一个最简单的Demo框架
    服务端高并发分布式架构演进之路
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254420.html
Copyright © 2011-2022 走看看