0%
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
| string Manacher(string s) { if (s.size() < 2) return s; stringstream ss; ss << '^'; for (int i=0; i<s.size(); i++) { ss << '#' << s[i]; } ss << '#' << '$'; string t = ss.str(); int n = t.size(); vector<int> p(n); int id = 0, mx = 0; int maxLength = -1; int index = 0; for (int j=1; j<n-1; j++) { p[j] = mx > j ? min(p[2*id-j], mx-j) : 1; while (t[j+p[j]] == t[j-p[j]]) { p[j]++; } if (mx < p[j] + j) { mx = p[j] + j; id = j; } if (maxLength < p[j] - 1) { maxLength = p[j] - 1; index = j; } } int start = (index-maxLength)/2; return s.substr(start, maxLength); }
|