zoukankan      html  css  js  c++  java
  • poj 3080 KMP

    Blue Jeans
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11564   Accepted: 4969

    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
    

    题目大意:找几个字串的最长公共子串。如小于3则输出no siginificant commonalities

    思路:

    用其他的字符串对每一个字串的后缀进行匹配,计录每个字串能匹配到的最大长度,然后所有串取min.

    再把所有后缀的min取个max即可

    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    int a[21][71];
    int s[71],next[71];
    int m,ap,ple,xzq,n,i,j,k,x,z,q,t,l,T;
    char c;
    bool p;
    
    void Read()
    {
        m=0;
        while(c=getchar(),c<'A'||c>'Z');
        a[i][++m]=c;
        while(c=getchar(),c>='A'&&c<='Z'){
            a[i][++m]=c;
            if(m==60)break;
        }
    }
    
    bool compare(int x,int z,int q)
    {
        int i;
        for(i=1;i<=q;i++){
            if(a[1][x+i-1]<a[1][z+i-1])return true;
            if(a[1][x+i-1]>a[1][z+i-1])return false;
        }
        return false;
    }
    
    int main()
    {
        scanf("%d",&t);
        for(T=1;T<=t;T++){
            memset(a,0,sizeof(a));
            xzq=0;
            scanf("%d",&n);
            for(i=1;i<=n;i++){
                Read();
            }
            for(i=1;i<=58;i++){
                for(j=i;j<=60;j++)s[j-i+1]=a[1][j];
                l=60-i+1;
                memset(next,0,sizeof(next));
                x=0;
                for(j=2;j<=l;j++){
                    while(x!=0&&s[j]!=s[x+1])x=next[x];
                    if(s[j]==s[x+1])x++;
                    next[j]=x;
                }
                ple=0;
                for(j=2;j<=n;j++){
                    ap=0;
                    x=0;
                    for(k=1;k<=60;k++){
                        while(x!=0&&a[j][k]!=s[x+1])x=next[x];
                        if(a[j][k]==s[x+1])x++;
                        if(x>ap)ap=x;
                        if(x==l)x=next[x];
                    }
                    if(ple==0||ap<ple)ple=ap;
                }
                if(ple>xzq){
                    xzq=ple;
                    z=i;
                    q=ple;
                }
                else if(ple==xzq){
                    p=compare(z,i,ple);
                    if(p==false){
                        z=i;
                        q=ple;
                    }
                }
                
            }
            if(xzq<3)printf("no significant commonalities
    ");
            else{
                for(i=z;i<z+q;i++)printf("%c",a[1][i]);
                printf("
    ");
            }
        }
    }
  • 相关阅读:
    JAVA开发人员画图表总结(ECHARTS)
    Spring Validation 表单校验
    Java BIO、NIO、AIO 学习
    JAVA笔试题
    JAVA GC优化入门
    jstat 使用日志
    JAVA内存泄漏
    JAVA 线程池入门事例
    JAVA Semaphore
    Serializable 介绍
  • 原文地址:https://www.cnblogs.com/applejxt/p/3801565.html
Copyright © 2011-2022 走看看