#include <iostream>
#include <cstring>
#include <cmath>
#define maxn 1010
using namespace std;
char help[maxn];
void ManacherString(string str);
int MaxLcpsLength(string str);
int MaxLcpsLength(string str)
{
int len = (int)str.length() * 2 + 1;
ManacherString(str);
if(len == 0)return 0;
int Arr[len];//回文半径
int C = -1;//中心
int R = -1;//回文右边界,最大右有效区为r-1
int Max = INT_MIN;//cout<<Max<<endl;
for(int i = 0;i < len;i++)
{
Arr[i] = R > i?min(Arr[2*C-1],R-i): 1 ;
while(i + Arr[i] < len&&i - Arr[i] > -1)
{
if(help[i + Arr[i]] == help[i - Arr[i]])
Arr[i]++;
else break;
}
if(i + Arr[i] > R)
{
R = i + Arr[i];
C = i;
}
Max = max(Max,Arr[i]);
}
return Max - 1;
}
void ManacherString(string str)
{
memset(help,0,sizeof(help));
int len = (int)str.length() *2+1;
int index = 0;
for(int i = 0;i < len;i++)
{
help[i] = i%2 == 0?'#':str[index++];
}
for(int i = 0;i < len;i++)cout<<help[i]<<endl;
}
int main()
{
cout << MaxLcpsLength("bbbaaacddcaaa")<< endl;
return 0;
}
Manacher模板(回文字符串)
猜你喜欢
转载自blog.csdn.net/qq_42937838/article/details/104397772
今日推荐
周排行