Blue Jeans
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14307 | Accepted: 6368 |
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.
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 AGATACCATCATCAT
题意:给你N个串,让你找出最长公共子串(长度必须大于2)。若有长度相同的子串,输出字典序较小的。
无语死了,一直以为考察后缀数组,没想到KMP暴力也可以过。
1A之后,好后悔自己没有去尝试。
思路:枚举一个串的所有子串,判断该子串有没有在其它串中出现。若在其它串中全部出现 则更新子串,否则枚举下一个子串。
这周把后缀数组看了 fighting !
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char str[10][70]; int f[70]; void getfail(char *s) { int len = strlen(s); f[0] = f[1] = 0; for(int i = 1; i < len; i++) { int j = f[i]; while(j && s[i] != s[j]) j = f[j]; f[i+1] = s[i]==s[j] ? j+1 : 0; } } bool find(char *a, char *b)//判断 b串在不在a串 { int la = strlen(a); int lb = strlen(b); int j = 0; for(int i = 0; i < la; i++) { while(j && a[i] != b[j]) j = f[j]; if(a[i] == b[j]) j++; if(j >= lb) return true; } return false; } int main() { int t, N; scanf("%d", &t); while(t--) { scanf("%d", &N); for(int i = 0; i < N; i++) scanf("%s", str[i]); int l = strlen(str[0]); char ss[70];//枚举 匹配的串 char ans[70];//结果 串 memset(ans, '\0', sizeof(ans)); for(int i = 0; i < l-2; i++)//枚举串的起点 { for(int j = i+2; j < l; j++)//枚举串的终点 { int p = 0; for(int k = i; k <= j; k++)//存储新子串 ss[p++] = str[0][k]; ss[p] = '\0'; getfail(ss); bool flag = true; for(int k = 1; k < N; k++) { if(!find(str[k], ss)) { flag = false;//失败 break; } } if(flag)//成功 更新 { if(strlen(ss) > strlen(ans))//比较长度 strcpy(ans, ss); else if(strlen(ss) == strlen(ans))//比较字典序 { if(strcmp(ss, ans) < 0) strcpy(ans, ss); } } } } if(strlen(ans) == 0) printf("no significant commonalities\n"); else printf("%s\n", ans); } return 0; }