POJ 3080 Blue Jeans

Blue Jeans
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 12142
Accepted: 5260

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

South Central USA 2006


题目链接  :http://poj.org/problem?id=3080

题目大意  :给若干主串,求其最长公共连续子串,若长度小于3,输出无解

题目分析  :和这道题算法与Corporate Identity相同,就直接贴代码了


#include <cstdio>
#include <cstring>
int const MAX = 61;
char text[10][MAX];
char subtext[MAX];
char res[MAX];
int next[MAX];
int num;
int sublen;
int max;

void get_next()  
{
    int i = 0, j = -1;
    next[0] = -1;
    while(subtext[i] != '\0')
    {
        if(j == -1 || subtext[i] == subtext[j])
        {
            j++;
            i++;
            if(subtext[i] == subtext[j])
                next[i] = next[j];
            else
                next[i] = j;
        }
        else
            j = next[j];
    }
}

void KMP()
{
    memset(next,0,sizeof(next));
    get_next();
    int cur;
    max = 10000;
    for(int i = 1; i < num; i++)
    {
        cur = 0;
        int k = 0, j = 0;
        while(j < strlen(text[i]) && k < sublen)
        {
            if(k == -1 || text[i][j] == subtext[k])
            {
                j++;
                k++;
            }
            else
                k = next[k];
            if(k > cur)
                cur = k;
        }
        if(cur < max)
            max = cur;
    }
}

int main()
{   
    int T ,ans;
    scanf("%d",&T);
    while(T--)
    {
        ans = 0;
        memset(text,0,sizeof(text));
        scanf("%d",&num);
        for(int i = 0; i < num; i++)
            scanf("%s",text[i]);
        for(int i = 0; i < 61; i++)
        {
            strcpy(subtext, text[0] + i);
            sublen = 60 - i;
            KMP();
            if(ans < max && max > 2)  
            {
                ans = max;
                strncpy(res, text[0] + i, ans);
                res[ans] = '\0';
            }
            else if(ans == max)
            {
                char temp[61];
                strncpy(temp, text[0] + i, ans);
                temp[ans] = '\0';
                if(strcmp(res,temp) > 0)
                strcpy(res,temp);
            }
            
        }
        if(ans == 0)
            printf("no significant commonalities\n");
        else
            printf("%s\n",res);
    }
}


你可能感兴趣的:(KMP,poj)