Blue Jeans 【KMP+暴力】

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

题目大意:给出n串字符串,求每一串都含有的最长子序列,并且如果有多个答案,输出字母序靠前的那个;

思路:根据题意可知,这道题数据范围很小,每个串长度为六十,所以直接暴力,枚举第一串的所有子串,然后判断在后面的串中是否都出现过;

上代码:

由于数据比较水,这个代码有点错误也AC了;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=105;

int Next[15][N];
char s[15][N];
char tmp[N];//临时串
char ans[N];//答案串

void get_Next(int m)
{
    int i=0,j=-1;
    Next[m][0]=-1;
    while(i<60)
    {
        if(j==-1||s[m][i]==s[m][j])
            Next[m][++i]=++j;
        else
            j=Next[m][j];
    }
}

int kmp(int n,int m)//KMP核心代码,如果能找到返回true;
{
    int i=0,j=0;
    while(i<60&&j<n)
    {
        if(j==-1||tmp[j]==s[m][i])
        {
            i++;
            j++;
        }
        else
            j=Next[m][j];
    }
    if(j>=n)
        return true;
    else
        return false;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(Next,0,sizeof(Next));
        memset(ans,'\0',sizeof(ans));
        int i,n,j,t,l,k,flag=0,vis1,vis2,len;
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            scanf("%s",s[i]);
            get_Next(i);//直接得到每一串的Next数组,方便使用
        }
        for(i=59; i>=2; i--)//代表每次枚举当前串的长度
        {
            vis2=1;
            for(j=0; j+i<60; j++)
            {
                vis1=0;
                memset(tmp,'\0',sizeof(tmp));
                for(k=0; k<=i; k++)//将字串添加进临时串
                    tmp[k]=s[0][j+k];
                for(k=1; k<n; k++)
                {
                    if(!kmp(i+1,k))
                    {
                        vis1=1;
                        break;
                    }
                }
                if(vis1==0)
                {
                    vis2=0;
                    if(tmp[0]<ans[0]||ans[0]=='\0')//如果ans串为空或者字母序更大,复制临时串刀ans串
                        strcpy(ans,tmp);
                }
            }
            if(vis2==0)
            {
                flag=1;
                break;
            }
        }
        if(flag==0)
            printf("no significant commonalities\n");
        else
            printf("%s\n",ans);
    }
    return 0;
}

这个是正确代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;

int n;
char s[15][75];
int nex[75];
char c[75];

void get_nex(char s[])
{
    int k=-1;
    int j=0;
    nex[0]=-1;
    while(j<60)
    {
        if(k==-1||s[j]==s[k])
        {
            j++;
            k++;
            nex[j]=k;
        }
        else
            k=nex[k];
    }
}

bool KMP(char s[],char t[])
{
    int i=0,j=0;
    int len1=strlen(s);
    int len2=strlen(t);
    while(i<len1&&j<len2)
    {
        if(j==-1||s[i]==t[j])
        {
            i++;
            j++;
        }
        else
            j=nex[j];
    }
    if(j==len2)
        return true;
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int now=1;
        int tmp=60;
        for(int i=1; i<=n; i++)
            scanf("%s",s[i]);
        char ans[75]="";
        int flag=0;
        for(int len=tmp; len>=3; len--)
        {
            for(int i=0; i<=tmp-len; i++)
            {
                strncpy(c,s[now]+i,len);
                c[len]='\0';
                get_nex(c);
                int j=1;
                for(j=1; j<=n; j++)
                {
                    if(j==now) continue;
                    if(!KMP(s[j],c))
                    {
                        break;
                    }
                }
                if(j==n+1)
                {
                    flag=1;
                    if(strcmp(c,ans)<0||!strlen(ans))
                        strncpy(ans,c,strlen(c));
                }
            }
            if(flag) break;
        }
        if(flag==0)
            printf("no significant commonalities\n");
        else
            printf("%s\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41984014/article/details/81541741