1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <iostream> #include <vector> using namespace std; vector<int> kmptable(string &str) { int len = str.length(), j = 0; vector<int> table(len, 0); for(int i = 1; i < len; i++) { while(j && str[i] != str[j]) j = table[j-1]; if(str[i] == str[j]) table[i] = ++j; } return table; } bool kmp(string &s, string p) { int ret = 0; auto table = kmptable(p); int n = s.size(), m = p.size(), j = 0; for(int i = 0; i < n; i++) { while (j && s[i] != p[j]) j = table[j - 1]; if (s[i] == p[j]) if(j == m-1) { ret++; j = table[j]; if(ret >= 2) return true; } else j++; } return false; }
int binarysearch(string& s) { int l = 0, r = s.length(), m, ans = 0; while(l < r) { bool find = false; m = (l + r) / 2; for(int i = 0; i < s.length() - m && !find; i++) find = kmp(s, s.substr(i,m)); if(find) { l = m + 1; ans = max(ans, m); } else { r = m; } } return ans; } int main(int argc, char** argv){ string str; cin>>str; cout<<binarysearch(str); return 0; }
|