POJ 3080--Blue Jeans【KMP && 暴力枚举】

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14316   Accepted: 6374

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

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[15][100];
int next[100];

void getnext(char *s1){
    int j = 0, k = -1;
    int len = strlen(s1);
    next[0] = -1;
    while(j < len){
        if(k == -1 || s1[j] == s1[k]){
            ++j;
            ++k;
            next[j] = k;
        }
        else k = next[k];
    }
    return ;
}

bool kmp(char *s1, char *s2){
    int len1 = strlen(s2);
    int len2 = strlen(s1);
    getnext(s1);
    int i = 0, j = 0;
    while(i < len1){
        if(j == -1 || s1[j] == s2[i]){
            ++i;
            ++j;
        }
        else j=next[j];
        if(j == len2)
            return true;
    }
    return false;
}

int main(){
    int T;
    scanf("%d", &T);
    char temp[100];
    char sum[100];
    while(T--){
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; ++i)
            scanf("%s", str[i]);
        int len = strlen(str[0]);
        memset(sum, '\0', sizeof(sum));
        for(int i = 0; i < len; ++i){//枚举串的起点
            int ans = 0;
            for(int j = i; j < len; ++j){//枚举串的终点
                temp[ans++] = str[0][j];//存储新的串
                temp[ans] = '\0';
                int flag = 1;
                for(int k = 1; k < n; ++k){//新的串和str[]匹配
                    if(!kmp(temp, str[k])){//匹配失败
                        flag = 0;
                        break;
                    }
                }
                if(flag){//匹配成功
                    //最长公共子串而且字典序最小
                    if(strlen(temp) > strlen(sum))
                        strcpy(sum, temp);
                    else if(strlen(temp)==strlen(sum) && strcmp(temp, sum) < 0)
                        strcpy(sum, temp);
                }

            }
        }
        if(strlen(sum) < 3)
            printf("no significant commonalities\n");
        else
            printf("%s\n", sum);
    }
    return 0;
}


你可能感兴趣的:(POJ 3080--Blue Jeans【KMP && 暴力枚举】)