P3085(manacher 模板题)
题目链接:https://www.luogu.org/problemnew/show/P3805
AC代码:
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#define PI 3.1415926
#define maxx 200000000
using namespace std;
char p[maxx];
char t[maxx*2];
int l[maxx];
void manacher()
{
int len=strlen(t);
p[0]='$',p[1]='#';
int pos=2;
for(int i=0;i<len;i++)
{
p[pos]=t[i];
p[pos+1]='#';
pos+=2;
}
p[pos]='\0';
int id=0,reslen=0,rescen=0,mx=0;
for(int i=1;i<pos;i++)
{
l[i]=mx>i?min(l[2*id-i],mx-i):1;
while(p[l[i]+i]==p[i-l[i]])
++l[i];
if(mx-i<l[i])
{
mx=i+l[i];
id=i;
}
if(l[i]>reslen)
{
reslen=l[i];
rescen=i;
}
}
printf("%d\n",reslen-1);
}
int main()
{
scanf("%s",t);
manacher();
return 0;
}