题意:给你几个字符串,让你找他们的公共子串(len >= 3, 题上写了,生物也学过)
解题思路:我是拿每种case的第一个串的子串和后面串比较,注意,要按字典序排列,但是大佬说只用找后缀串就行了,至今都没看懂为什么,附上大佬链接,http://blog.sina.com.cn/s/blog_6635898a0100l4fg.html
AC代码:
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <cstdio> 6 #include <vector> 7 #define MAXSIZE 1000100 8 using namespace std; 9 10 vector<string> a; 11 string sub_a = "", res = ""; 12 int Next[MAXSIZE]; 13 int Len, Len_Res; 14 15 // int GetNext() 16 // { 17 // int i = 0, j = Next[0] = -1; 18 // while (i < Len) 19 // { 20 // if (j == -1 || a[i] == a[j]) 21 // Next[++i] = ++j; 22 // else 23 // j = Next[j]; 24 // } 25 // } 26 27 int main(void) 28 { 29 ios::sync_with_stdio(false); 30 int cas; 31 cin >> cas; 32 while (cas--) 33 { 34 int n; 35 cin >> n; 36 for (int i = 0; i < n; ++i) 37 { 38 string temp; 39 cin >> temp; 40 a.push_back(temp); 41 } 42 Len = a[0].size(); 43 44 for (int i = 0; i < Len - 3; ++i)//the origin of str 45 { 46 for (int j = 3; i + j <= Len; ++j)//the len of str 47 { 48 sub_a = a[0].substr(i, j); 49 int k; 50 for (k = 1; k < n; ++k) 51 { 52 if (a[k].find(sub_a) == -1) 53 break; 54 } 55 if (k == n) 56 { 57 if (sub_a.size() > res.size()) 58 res = sub_a; 59 else if (sub_a.size() == res.size() && sub_a < res) 60 res = sub_a; 61 } 62 } 63 } 64 65 if (res.size()) 66 cout << res << endl; 67 else 68 cout << "no significant commonalities" << endl; 69 70 //Don't forget it! 71 a.clear(); 72 res.clear(); 73 } 74 75 return 0; 76 }