• POJ 3080-Blue Jeans【kmp,字符串剪接】


    Blue Jeans
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20695   Accepted: 9167

    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
    

    Source

     

    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<iostream>
    #define nb main
    #define i6g3y int
    using namespace std;
    
    const int maxn = 100+10;
    const int INF = 0x3f3f3f3f;
    
    using namespace std;
    
    string s[100];
    
    int mynext[maxn];
    void getnext(string a,int len){
        int i,j;
        j=mynext[0]=-1;
        i=0;
        while(i<len){
            while(-1!=j&&a[i]!=a[j])j=mynext[j];
            mynext[++i]=++j;
        }
    }
    bool kmpcount(string a,int len1,string b,int len2){
        int i,j;
        i=j=0;
        while(i<len2){
            while(-1!=j&&a[j]!=b[i])j=mynext[j];
            i++;j++;
            if(j>=len1){
                return true;
            }
        }
        return false;
    }
    bool mycmp(string a,string b){
        if(a.length()>b.length())return true;
        else return b.compare(a);
    }
    i6g3y nb()
    {
        int t;
        std::ios::sync_with_stdio(false);
        cin>>t;
        while(t--){
            int n;
            cin>>n;
            for(int i=0;i<n;i++)cin>>s[i];
            string best="";
            bool flag=false;//标志这个长度的这个串有没有匹配
            bool Allflag=false;//标志这个长度有没有匹配
            int i;
            for(i=1;i<=60;i++){//长度
                Allflag=false; //假设这个长度不可以
                for(int first=0;first<60-i+1;first++){//起点
                    flag=true;//假设这个串可以
                    string test=s[0].substr(first,i);//模式串
    
                    getnext(test,i);//求next
                    for(int g=1;g<n;g++){
                        if(!kmpcount(test,i,s[g],60)){//如果有一个不匹配
                            flag=false;
                            break;
                        }
                    }
                    if(flag){//如果匹配
                        //cout<<"test     "<<test;
                        //cout<<"     可以匹配"<<endl;
                        Allflag=true;
                        if(mycmp(test,best)){
                            best=test;
                        }
                    }
                }
                if(!Allflag){
                    break;
                }
            }
            if(i<3)cout<<"no significant commonalities"<<endl;
            else cout<<best<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    自动机
    C语言文法
    实验报告一:词法分析
    Python的基础综合练习
    Python基础综合练习
    turtle画五星红旗
    熟悉常用的Linux操作
    大数据概述
    对学习编译原理的看法
    LINUX
  • 原文地址:https://www.cnblogs.com/luowentao/p/9399106.html
Copyright © 2020-2023  润新知