Blue Jeans POJ - 3080 (KMP 暴力枚举)

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86582820

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个字符串,求这些字符串的最长公共子串,最长公共子串的长度要大于等于3。

解题思路:因为数据范围比较小,而且每个字符串的长度都是60,所以以第一个字符串为基准,枚举第一个字符串的所有子串,并且求出每一个子串的Next数组与剩下的所有串进行KMP匹配即可。

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 100 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133 ;

char s[Maxn][Maxn] ;
int Next[Maxn] ;

void getNext(char *s){
    int len = strlen(s) ;
    Next[0] = -1 ;
    int j = 0, k = -1;
    while (j < len){
        if (k == -1 || s[j] == s[k]) Next[++j] = ++k ;
        else k = Next[k] ;
    }
}

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

int main (){
    int T, n ;
    cin >> T ;
    while (T--){
        cin >> n ;
        for (int i = 0; i < n; i++) cin >> s[i] ;
        char ans[Maxn] = "Z" ;
        int maxLen = 60 ;
        for (int len = 60; len >= 3; len--){
            for (int j = 0; j <= maxLen - len; j++){
                char b[Maxn] = {0} ;
                strncpy(b, s[0] + j, len) ;
//                cout << b << endl ;
                getNext(b) ;
                int i ;
                for (i = 1; i < n; i++){
                    if (!KMP(b, s[i])) break ;
                }
                if (i == n && strcmp(ans, b) > 0) strcpy(ans, b) ;
                if (ans[0] != 'Z' && j == maxLen - len) {
                    len = 0 ;
                    break ;
                }
            }
        }
        if (ans[0] == 'Z') cout << "no significant commonalities" << endl ;
        else cout << ans << endl ;
    }
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86582820