Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15357 | Accepted: 6823 |
Description
Input
Output
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
此题同 poj3450 只不过最后计算出的公共子串的长度要不小于3!
AC代码:
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #define mem(a, b) memset(a, (b), sizeof(a)) #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Pi(a) printf("%d\n", (a)) #define INF 0x3f3f3f3f #include<algorithm> #define N 100000+10 using namespace std; char str[11][70]; int p[70]; void getp(char *s) { int len = strlen(s); p[0] = p[1] = 0; for(int i = 1; i < len; i++) { int j = p[i]; while(j && s[i]!=s[j]) j = p[j]; p[i+1] = s[i]==s[j] ? j+1 : 0; } } bool find(char *a, char *b) { 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 = p[j]; if(a[i] == b[j]) j++; if(j >= lb) return true; } return false; } int main() { int t; Si(t); Wi(t) { int n; Si(n); for(int i = 0; i < n; i++) scanf("%s", str[i]); int l = strlen(str[0]); char ch[70], ans[70]; mem(ans, '\0'); for(int i = 0; i < l; i++) { for(int j = i; j < l; j++) { int m = 0; for(int k = i; k <= j; k++) ch[m++] = str[0][k]; ch[m] = '\0'; getp(ch); bool flag = 1; for(int k = 1; k < n; k++) { if(!find(str[k], ch)) { flag = 0; break; } } if(flag) { if(strlen(ch) > strlen(ans)) strcpy(ans, ch); else if(strlen(ch)==strlen(ans) && strcmp(ch, ans) < 0) strcpy(ans, ch); } } } if(strlen(ans) < 3) printf("no significant commonalities\n"); else printf("%s\n", ans); } return 0; }