POJ 3080 Blue Jeans 【KMP+DP】

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

题意:输出所给的字符串共有的最长子字符串,如果有多种解,输出字母排序最小的那个;

思路:我们取出第一个字符串S1,让这个字符串和其他的字符串一一比较,求出最长的那个公共子字符串;我们先取S1的第一个字符为首的子字符串,依次增加长度,然后通过KMP来判断是否能成为公共子字符串,以S1第一个字符为首的试完过后,再试以S1第二个字符为首的子字符串,以此类推;如果不用dp去记忆的话,有可能会超时;这里设置一个dp二维数组,i 为以S1的第i个字符为首,长度为j的字符串是否是一个公共子字符串,是的话dp【i】【j】= 1;否则为 0;那我们应该在什么时候去用这些记忆过的dp? 这里举个例子;假设 一个字符串 从i = 1开始 ,长度为 3 是所有字符串的一个公共子字符串,那么  i = 2的时候,也就是以S1第二个字符为首的,长度为 2 的字符串是否是所有字符串的公共子字符串,我们怎么判断?如果  a b c 是一个公共子字符串的话,那么 b c 是不是就一定是一个公共子字符串,不用而外用KMP去判断,直接访问上一次的dp数据就能够判断的了,如果为 0 的话,那就只能用 KMP去判断了,这样就可以省去不少时间; (代码很乱,主要看下思路就行了)

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

#define Maxn 65
int dp[Maxn][Maxn],Next[Maxn];
char s[15][Maxn],ans[Maxn],tmp[Maxn];

void getNext (char ptr[]) {
    memset(Next,0,sizeof(Next));
    int j = 0, k = -1, len = strlen(ptr);
    Next[0] = -1;

    while (j < len) {
        if(k == -1 || ptr[j] == ptr[k]) {
            if(ptr[++k] == ptr[++j]) {
                Next[j] = Next[k];
            } else Next[j] = k;
        } else k = Next[k];
    }
}

bool kmp (char str[], char ptr[]) {
    int i = 0, j = 0,len_str = strlen(str), len_ptr = strlen(ptr);
    getNext (ptr);

    while (i < len_str) {
        if(j == -1 || str[i] == ptr[j]) {
            ++j; ++i;
            if(j == len_ptr) return true;
        } else j = Next[j];
    }
    return false;
}
 
void sovle (char *tmp, char *ans) {  
    int len_1 = strlen(tmp),len_2 = strlen(ans);

    if(len_1 > len_2) strcpy(ans,tmp);  
    else if(len_1 == len_2) {
        for (int i = 0; i < len_1; ++i) {  // 判断字典顺序的
            if(tmp[i] < ans[i]) strcpy(ans,tmp);
            else if (tmp[i] > ans[i]) break;
        }
    }
}

int main(void)
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int t,n;
    scanf("%d",&t);
    while (t--) {
        scanf("%d",&n);
        memset(s,0,sizeof(s));
        memset(dp,0,sizeof(dp));
        for (int i = 1; i <= n; ++i) scanf(" %s",s[i]);
        int len_ = strlen(s[1]);
        int ok;
        memset(ans,0,sizeof(ans));
        for (int i = 1; i <= len_; ++i) {
                ok = true;
            for (int j = 1; j <= len_-(i-1); ++j) {
                if(dp[i-1][j+1]) { dp[i][j] = 1; continue; }  //访问上一次的dp数据,是否为1
                memset(tmp,0,sizeof(tmp));
                memcpy (tmp,s[1]+i-1,j);  // 在s中指定的那部分字符串复制到tmp
                for (int k = 2; k <= n; ++k) {  // 比较除第一个字符串以外的其他需要判断的字符串
                    if(kmp(s[k],tmp)) dp[i][j] = 1;
                    else { ok = false; break; }
                }
                if(!ok) break;
                sovle(tmp,ans);
            }
        }
        if(strlen(ans) < 3) printf("no significant commonalities\n");
        else printf("%s\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/godleaf/article/details/81200797
今日推荐