思路:
manacher模板。
实现:
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 int p[2000005]; 5 string init(string s) 6 { 7 string res = "$#"; 8 for (int i = 0; i < s.length(); i++) 9 { 10 res += s[i]; res += '#'; 11 } 12 return res; 13 } 14 int main() 15 { 16 int n; 17 cin >> n; 18 string s; 19 while (n--) 20 { 21 cin >> s; 22 memset(p, 0, sizeof p); 23 s = init(s); 24 int r = 0, id = 0, maxn = 0; 25 for (int i = 1; i < s.length(); i++) 26 { 27 if (i < r) p[i] = min(r - i, p[2 * id - i]); 28 else p[i] = 1; 29 int j = p[i]; 30 while (s[i + j] == s[i - j]) { j++; p[i]++; } 31 if (i + p[i] - 1 > r) { r = i + p[i] - 1; id = i; } 32 maxn = max(maxn, p[i] - 1); 33 } 34 cout << maxn << endl; 35 } 36 return 0; 37 }