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.
样例输入
13
22
3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
53
6GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
7GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
8GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
93
10CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
11ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
12AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
样例输出
1no significant commonalities
2AGATAC
3CATCATCAT
提示
无
思路
最多10个串,每个串最长60,n^3暴力即可。
代码
1char s[15][mxn], t[mxn], ans[mxn];
2int nxt[mxn];
3
4void getnxt(char* t, int m)
5{
6 int i = 0, j = -1; nxt[0] = -1;
7 while (i < m)
8 {
9 if (j == -1 || t[i] == t[j]) {
10 i++, j++;
11 // if (t[i] == t[j])
12 // nxt[i] = nxt[j]; // next数组优化
13 // else
14 nxt[i] = j;
15 } else
16 j = nxt[j];
17 }
18}
19
20int KMP(char* s, char* t, int n, int m)
21{
22 int i = 0, j = 0, ans = 0;
23 while (i < n)
24 {
25 if (j == -1 || s[i] == t[j]) {
26 i++, j++;
27 if (j >= m) { // 匹配
28 // ans++;
29 // j = nxt[j];
30 return i-j;
31 }
32 } else
33 j = nxt[j];
34 }
35 // return ans;
36 return -1;
37}
38
39int main()
40{
41 int T; scanf("%d", &T);
42 while(T--)
43 {
44 int n; scanf("%d", &n);
45 for(int i=0; i<n; i++) scanf("%s", s[i]);
46 int sl = strlen(s[0]), f=0;
47
48 for(int i=0; i<sl; i++) // 枚举模式串起点
49 {
50 int tl = 0, k;
51 for(int j=i; j<sl; j++) // 枚举模式串长度
52 {
53 t[tl++] = s[0][j];
54 t[tl] = '\0';
55 getnxt(t, tl);
56 for(k=0; k<n; k++) // 枚举所有文本串
57 if(KMP(s[k], t, sl, tl) == -1)
58 break;
59 if(k>=n) // 所有串公共子串
60 {
61 if(!f || tl>strlen(ans)){ // 长度最长
62 strcpy(ans, t); f=1;
63 }else if(tl==strlen(ans) && strcmp(t, ans)<0){ // 字典序最小
64 strcpy(ans, t);
65 }
66 }
67 }
68 }
69 if (!f || strlen(ans)<3)
70 printf("no significant commonalities\n");
71 else
72 printf("%s\n", ans);
73 }
74 return 0;
75}