Blue Jeans poj 3080(暴力&kmp)

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输出-1.

思路:因为题目给的数据不大,所以暴力也可以过,只需要枚举公共字符串的长度,然后遍历第一串每一位字符,然后到其他所有串中寻找相同子串即可。这题的正解是kmp,枚举第一串的所有后缀,每次把他的后缀看作是模式串,其他串看成是文本串,求得next数组后便开始和其他串进行匹配,因为要求的是公共子串,跟其他串中最小的子串就是这个后缀的与其他串的最长公共子串,最后遍历每一个后缀找到最长的公共子串就是答案了。

//暴力
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
char s[12][66],a[66],b[66];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        int ans=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=3;i<=60;i++)//公共子串的长度
        {
            for(int j=0;j<=60-i;j++)//遍历每一位
            {
                int k;
                strncpy(a,s[0]+j,i);//这个函数不会在最侯以为加上结束符
                a[i]='\0';//这里一定要把最后一位手动加上结束符
                for(k=1;k<n;k++)
                {
                    if(!strstr(s[k],a))
                        break;
                }
                if(k==n)
                {
                    if(strlen(a)==ans)
                    {
                        if(strcmp(b,a)>0)
                            strcpy(b,a);
                    }
                    else
                    {
                        ans=i;
                        strcpy(b,a);
                    }
                }
            }
        }
        if(ans>=3) printf("%s\n",b);
        else printf("no significant commonalities\n");
    }
}
//kmp
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int n,net[66],lenb,lena,ans;
char s[12][66],a[66],b[66];
void getnext()
{
    net[0]=-1;
    int k=-1,j=0;
    while(j<lenb)
    {
        if(k==-1||b[j]==b[k])
            net[++j]=++k;
        else k=net[k];
    }
}
void kmp()
{
    int i,j,tmp=0;
    getnext();
    ans=66;
    for(int k=1;k<n;k++)
    {
        tmp=i=j=0,lena=strlen(s[k]);
        while(i<60&&j<lenb)
        {
            if(j==-1||s[k][i]==b[j])
            {
                i++;
                j++;
            }
            else j=net[j];
            if(j>tmp) tmp=j;
        }
        ans=min(ans,tmp);//与其他n-1串字符串找到最小的相同子串
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int res=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<=57;i++)
        {
            strcpy(b,s[0]+i);
            lenb=60-i;
            kmp();
            if(ans>res)
            {
                res=ans;
                strncpy(a,b,ans);//这个函数复制时不会在末尾加上结束符
                a[ans]='\0';//一定要手动加上结束符,否则会wa
            }
            else if(ans==res)
            {
                char tt[66];
                strncpy(tt,b,ans);
                tt[ans]='\0';
                if(strcmp(a,tt)>0)
                    strcpy(a,tt);
            }
        }
        if(res<3) printf("no significant commonalities\n");
        else printf("%s\n",a);
    }
}

猜你喜欢

转载自blog.csdn.net/never__give__up/article/details/80514465