【题解】POJ-3080 Blue Jeans

Blue Jeans (POJ-3080)

题面

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 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.

输出

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.

样例输入

1
2
3
4
5
6
7
8
9
10
11
12
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

样例输出

1
2
3
no significant commonalities
AGATAC
CATCATCAT

提示

思路

最多10个串,每个串最长60,n^3暴力即可。

代码

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
char s[15][mxn], t[mxn], ans[mxn];
int nxt[mxn];

void getnxt(char* t, int m)
{
int i = 0, j = -1; nxt[0] = -1;
while (i < m)
{
if (j == -1 || t[i] == t[j]) {
i++, j++;
// if (t[i] == t[j])
// nxt[i] = nxt[j]; // next数组优化
// else
nxt[i] = j;
} else
j = nxt[j];
}
}

int KMP(char* s, char* t, int n, int m)
{
int i = 0, j = 0, ans = 0;
while (i < n)
{
if (j == -1 || s[i] == t[j]) {
i++, j++;
if (j >= m) { // 匹配
// ans++;
// j = nxt[j];
return i-j;
}
} else
j = nxt[j];
}
// return ans;
return -1;
}

int main()
{
int T; scanf("%d", &T);
while(T--)
{
int n; scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%s", s[i]);
int sl = strlen(s[0]), f=0;

for(int i=0; i<sl; i++) // 枚举模式串起点
{
int tl = 0, k;
for(int j=i; j<sl; j++) // 枚举模式串长度
{
t[tl++] = s[0][j];
t[tl] = '\0';
getnxt(t, tl);
for(k=0; k<n; k++) // 枚举所有文本串
if(KMP(s[k], t, sl, tl) == -1)
break;
if(k>=n) // 所有串公共子串
{
if(!f || tl>strlen(ans)){ // 长度最长
strcpy(ans, t); f=1;
}else if(tl==strlen(ans) && strcmp(t, ans)<0){ // 字典序最小
strcpy(ans, t);
}
}
}
}
if (!f || strlen(ans)<3)
printf("no significant commonalities\n");
else
printf("%s\n", ans);
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/