POJ 3080 Blue Jeans

Blue Jeans

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  PKU. Original ID: 3080
64-bit integer IO format: %lld      Java class name: Main
 
 
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

 
解题:暴力查找长度最长,字典序最小的公共子串。
 
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define pii pair<int,int>

15 #define INF 0x3f3f3f3f

16 using namespace std;

17 char str[12][100];

18 int n,fail[100],len = 60;

19 void getFail(char *s,int m) {

20     fail[0] = fail[1] = 0;

21     for(int i = 1; i < m; i++) {

22         int j = fail[i];

23         while(j && s[i] != s[j]) j = fail[j];

24         fail[i+1] = str[i] == str[j]?j+1:0;

25     }

26 }

27 bool kmp(char *s,int m,int k) {

28     getFail(s,m);

29     for(int i = 0,j = 0; i < len; i++) {

30         while(j && s[j] != str[k][i]) j = fail[j];

31         if(str[k][i] == s[j]) {

32             j++;

33             if(j == m) return true;

34         }

35     }

36     return false;

37 }

38 int main() {

39     char tmp[100],ans[100];

40     int i,j,k,t;

41     bool flag;

42     scanf("%d",&t);

43     while(t--) {

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

45         flag = false;

46         for(i = 0; i < n; i++)

47             scanf("%s",str[i]);

48         len = strlen(str[0]);

49         strcpy(ans,str[0]);

50         for(i = len; i > 2 && !flag; i--) {

51             for(j = 0; j+i <= len; j++) {

52                 strncpy(tmp,str[0]+j,i);

53                 tmp[i] = '\0';

54                 for(k = 1; k < n; k++)

55                     if(!kmp(tmp,i,k)) break;

56                 if(k == n) {

57                     flag = true;

58                     if(strcmp(ans,tmp) > 0) strcpy(ans,tmp);

59                     else strcpy(ans,tmp);

60                 }

61             }

62         }

63         flag?puts(ans):puts("no significant commonalities");

64     }

65     return 0;

66 }
View Code

 

你可能感兴趣的:(poj)