Blue Jeans 【KMP+暴力】

幾許情愁發表於2018-08-09

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;
}

 

相關文章