POJ3080 Blue jeans(未完待续,KMP,寻找多个子串的公共串)

来源:互联网 发布:java短信验证码代码 编辑:程序博客网 时间:2024/06/07 03:26

POJ3080
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

题意:求n个字符串的最长公共串。
求n个字符长度最长公共子串。对于多模式匹配问题,一般是不可以用KMP解决得,因为忒暴力。
思路很简单:我们先按字符串的长度由短到长进行快排。枚举第一个字符串的不同长度子串,判断她是否为下面多有的公共子串?如果是的话,那么我们就表明找到,则比较其长度,如果比已经找到的串长,那么就替换结果串 否则按字典序比较。取字典序考前的,就可以。

KMP:

#include<iostream>#include<string.h>#include<cstring>#include<cstdio>#define maxn 62using namespace std;int next[maxn],n,ma,lenp;char text[10][maxn],pat[maxn];void getNext(){    int i=0,j=-1;    next[0]=-1;    while(i<lenp){        if(j==-1||pat[i]==pat[j]){            i++,j++;            next[i]=j;        }        else j=next[j]; //寻找更短的前缀    }}void kmp(){       int i,j,k,m;    getNext();    ma=100;    for(k=1;k<n;k++){ //有n-1次字符串比较        i=0;j=0;m=0;        while(i<60 &&j<lenp){            if(j==-1 ||text[k][i]==pat[j])  {                i++,j++;            }            else j=next[j]; //匹配失败,字符串又移next[j]            if(j>m) m=j;        }        if(m<ma) ma=m;    }}int main(){    int t,i;    char result[maxn];    cin>>t;    while(t--){        cin>>n;        for(i=0;i<n;i++)            scanf("%s",text[i]);        int ans=0;        for(i=0;i<60;i++){            strcpy(pat,text[0]+i);  //枚举第一个串所有的后缀串            lenp=60-i;            kmp();   //kmp求出这个后缀串与其余所有串的最大匹配            if(ans<ma){                ans=ma;                //把以text[0]+i开头的ans个元素放到result中                strncpy(result,text[0]+i,ans);                result[ans]='\0';            }            else if(ans==ma){  //存在多个公共字符串,输出字典序最小的                char tmp[maxn];                strncpy(tmp,text[0]+i,ans); //strncpy没有复制最后的\0                tmp[ans]='\0';                if(strcmp(tmp,result)<0)  //字典排序                    strcpy(result,tmp);            }        }        if(ans>=3)            printf("%s\n",result);        else             printf("no significant commonalities\n");    }    return 0;}
原创粉丝点击