#include<bits/stdc++.h>
using namespace std;
const int N = 1e7 + 1e6 + 5;
int p[N << 1 + 5]; //数组大小要翻倍!!
int main() {
string str; cin >> str;
string s = "$#";
for (int i = 0; i < str.size(); i++) {
s += str[i]; s += "#";
}
int mx = 0, id = 0, ans = 1;
for (int i = 0; i < s.size(); i++) {
p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
while (i - p[i] >= 0 && i + p[i] < s.size() && s[i - p[i]] == s[i + p[i]]) p[i]++; //注意边界!!!
if (i + p[i] > mx) mx = i + p[i], id = i;
ans = max(ans, p[i] - 1);
}
cout << ans << endl;
return 0;
}
【例题记录】马拉车Manacher模板
最新推荐文章于 2024-07-18 17:40:16 发布