#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <assert.h>
#include <set>
#include <xfunctional>
using namespace std;
int manacher(string str) {
int maxRight = 0, pos = 0, maxlen = 0;
int *RL = new int[str.size()];
RL[0] = 0;
for (int i = 1; i < str.size(); i++) {
if (i < maxRight) {
RL[i] = min(maxRight - i, RL[2 * pos - i]); //2*pos - i;
maxlen = max(RL[i], maxlen);
}
else
RL[i] = 1;
while(str[RL[i] + i] == str[i - RL[i]] && RL[i] + i < str.size() && i - RL[i] >= 0) //i - RL[i]
RL[i]++;
maxlen = max(RL[i], maxlen);
if (RL[i] + i - 1 > maxRight) {
maxRight = RL[i] + i - 1;
pos = i;
}
}
return maxlen - 1;
}
int main() {
string str;
cin >> str;
string str1 = "$#";
for (int i = 0; i < str.size(); i++) {
str1 += str[i];
str1 += "#";
}
cout << str.size() - manacher(str1);
}
数据结构与算法--manacher算法
最新推荐文章于 2025-03-02 20:43:06 发布