题目大意:给你m串字符串,要你找最长的相同的连续字串
这题暴力kmp即可,注意要按字典序排序,同时,是len<3才输出no significant commonalities
1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 #include <string.h> 5 #define MAX 60 6 7 using namespace std; 8 9 typedef char* _String; 10 typedef int Position; 11 static char str[11][61],ans[62]; 12 static int _next[62]; 13 14 bool KmpSearch(_String, _String, const int); 15 void Get_Next(_String, const int); 16 void Search(const int); 17 18 int main(void)//暴力枚举第一行 19 { 20 int case_sum, m; 21 //freopen("in.txt", "r", stdin); 22 scanf("%d", &case_sum); 23 24 while (case_sum--) 25 { 26 scanf("%d", &m); 27 getchar(); 28 29 for (int i = 0; i < m; i++) 30 scanf("%s", str[i]); 31 Search(m); 32 } 33 return EXIT_SUCCESS; 34 } 35 36 void Search(const int m) 37 { 38 int len, ans_len = -1, pos, if_match; 39 char tmp; 40 41 for (len = 1; len <= MAX; len++) 42 { 43 for (pos = 0; pos + len <= MAX; pos++) 44 { 45 if_match = 0; 46 Get_Next(&str[0][pos], len); 47 for (int i = 1; i < m; i++) 48 if_match += KmpSearch(str[i], &str[0][pos], len); 49 if (if_match == m - 1 && len >= ans_len) 50 { 51 if (len == ans_len) 52 { 53 tmp = str[0][pos + len]; 54 str[0][pos + len] = '