Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12142 | Accepted: 5260 |
Description
Input
Output
Sample Input
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities
AGATAC
CATCATCAT
Source
#include <cstdio> #include <cstring> int const MAX = 61; char text[10][MAX]; char subtext[MAX]; char res[MAX]; int next[MAX]; int num; int sublen; int max; void get_next() { int i = 0, j = -1; next[0] = -1; while(subtext[i] != '\0') { if(j == -1 || subtext[i] == subtext[j]) { j++; i++; if(subtext[i] == subtext[j]) next[i] = next[j]; else next[i] = j; } else j = next[j]; } } void KMP() { memset(next,0,sizeof(next)); get_next(); int cur; max = 10000; for(int i = 1; i < num; i++) { cur = 0; int k = 0, j = 0; while(j < strlen(text[i]) && k < sublen) { if(k == -1 || text[i][j] == subtext[k]) { j++; k++; } else k = next[k]; if(k > cur) cur = k; } if(cur < max) max = cur; } } int main() { int T ,ans; scanf("%d",&T); while(T--) { ans = 0; memset(text,0,sizeof(text)); scanf("%d",&num); for(int i = 0; i < num; i++) scanf("%s",text[i]); for(int i = 0; i < 61; i++) { strcpy(subtext, text[0] + i); sublen = 60 - i; KMP(); if(ans < max && max > 2) { ans = max; strncpy(res, text[0] + i, ans); res[ans] = '\0'; } else if(ans == max) { char temp[61]; strncpy(temp, text[0] + i, ans); temp[ans] = '\0'; if(strcmp(res,temp) > 0) strcpy(res,temp); } } if(ans == 0) printf("no significant commonalities\n"); else printf("%s\n",res); } }