Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14316 | Accepted: 6374 |
Description
Input
Output
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char str[15][100]; int next[100]; void getnext(char *s1){ int j = 0, k = -1; int len = strlen(s1); next[0] = -1; while(j < len){ if(k == -1 || s1[j] == s1[k]){ ++j; ++k; next[j] = k; } else k = next[k]; } return ; } bool kmp(char *s1, char *s2){ int len1 = strlen(s2); int len2 = strlen(s1); getnext(s1); int i = 0, j = 0; while(i < len1){ if(j == -1 || s1[j] == s2[i]){ ++i; ++j; } else j=next[j]; if(j == len2) return true; } return false; } int main(){ int T; scanf("%d", &T); char temp[100]; char sum[100]; while(T--){ int n; scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%s", str[i]); int len = strlen(str[0]); memset(sum, '\0', sizeof(sum)); for(int i = 0; i < len; ++i){//枚举串的起点 int ans = 0; for(int j = i; j < len; ++j){//枚举串的终点 temp[ans++] = str[0][j];//存储新的串 temp[ans] = '\0'; int flag = 1; for(int k = 1; k < n; ++k){//新的串和str[]匹配 if(!kmp(temp, str[k])){//匹配失败 flag = 0; break; } } if(flag){//匹配成功 //最长公共子串而且字典序最小 if(strlen(temp) > strlen(sum)) strcpy(sum, temp); else if(strlen(temp)==strlen(sum) && strcmp(temp, sum) < 0) strcpy(sum, temp); } } } if(strlen(sum) < 3) printf("no significant commonalities\n"); else printf("%s\n", sum); } return 0; }