KMP模式匹配算法:Blue Jeans

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

 

  1 #include <cstdio>

  2 #include <iostream>

  3 #include <cstring>

  4 #include <string>

  5 #include <algorithm>

  6 

  7 using namespace std;

  8 

  9 const int MAX = 64;

 10 const int MMAX = 2048;

 11 

 12 int tcut;

 13 char s[MAX][MAX];

 14 char t[MMAX][MAX];

 15 char ans[MAX];

 16 

 17 int Tlen, Next[MAX];

 18 char T[MAX];

 19 

 20 void getNext();

 21 int kmp(char *S);

 22 

 23 int main()

 24 {

 25 #ifdef OFFLINE

 26     //freopen("in.txt", "r", stdin);

 27     //freopen("out.txt", "w", stdout);

 28 #endif

 29 

 30     int n;

 31     scanf("%d", &n);

 32     while (n--)

 33     {

 34         tcut = 0;

 35         memset(s, 0, sizeof(s));

 36         memset(t, 0, sizeof(t));

 37         memset(ans, 0, sizeof(ans));

 38 

 39         int m;

 40         scanf("%d", &m);

 41         for (int i = 0; i < m; i++)

 42         {

 43             scanf("%s", s[i]);

 44         }

 45 

 46         //Create String T Array

 47         for (int i = 3; i <= 60; i++)

 48         {

 49             for (int j = 0; j <= 60 - i; j++)

 50             {

 51                 memcpy(t[tcut++], s[0] + j, i);

 52             }

 53         }

 54         

 55         for (int i = 0; i < tcut; i++)

 56         {

 57             Tlen = strlen(t[i]);

 58             strcpy(T, t[i]);

 59             memset(Next, 0, sizeof(Next));

 60 

 61             getNext();

 62 

 63 

 64             bool bStatus = true;

 65             for (int j = 0; j < m; j++)

 66             {

 67                 if(kmp(s[j]) == -1)

 68                 {

 69                     bStatus = false;

 70                     break;

 71                 }

 72             }

 73 

 74             if (bStatus)

 75             {

 76                 if (Tlen > strlen(ans) || (Tlen == strlen(ans) && strcmp(t[i], ans) < 0))

 77                 {

 78                     strcpy(ans, t[i]);

 79                 }

 80             }

 81         }

 82         

 83         printf("%s\n", strlen(ans) ? ans : "no significant commonalities");

 84     }

 85 

 86     return 0;

 87 }

 88 

 89 void getNext()

 90 {

 91     int i = 0, j = -1;

 92     Next[0] = -1;

 93     while (i < Tlen)

 94     {

 95         if (j == -1 || T[i] == T[j])

 96             Next[++i] = ++j;

 97         else

 98             j = Next[j];

 99     }

100 }

101 

102 int kmp(char *S)

103 {

104     int Slen = strlen(S);

105     int i = 0, j = 0;

106 

107     while(i <= Slen)

108     {

109         while(j != -1 && S[i] != T[j])

110         {

111             j = Next[j];

112         }

113 

114         if(j == Tlen - 1)

115         {

116             return i - Tlen + 1;

117         }

118         else

119         {

120             j++; i++;

121         }

122     }

123 

124     return -1;

125 }
View Code

 

你可能感兴趣的:(KMP)