当前位置: 代码迷 >> 综合 >> bzoj2565(manacher)
  详细解决方案

bzoj2565(manacher)

热度:83   发布时间:2024-01-04 12:45:19.0

利用了manacher的整个思想

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=200005;int n,m,len,f[N*2],front[N*2],back[N*2];
char ch[N],s[N*2];int main()
{scanf("%s",ch);n=strlen(ch);s[len++]='$';for (int i=0;i<n;i++) s[len++]='#',s[len++]=ch[i];s[len++]='#';int id=0,mx=0;for (int i=1;i<len;i++){if (i<=mx) f[i]=min(f[id-(i-id)],mx-i+1);else f[i]=1;while (s[i+f[i]]==s[i-f[i]]) f[i]++;if (i+f[i]-1>mx){for (int j=mx+1;j<=i+f[i]-1;j++) front[j]=i;mx=i+f[i]-1;id=i;}}id=len,mx=len;for (int i=len-1;i>0;i--){if (i>=mx) f[i]=min(f[id+(id-i)],i-mx+1);else f[i]=1;while (s[i+f[i]]==s[i-f[i]]) f[i]++;if (i-f[i]+1<mx){for (int j=mx-1;j>=i-f[i]+1;j--) back[j]=i;mx=i-f[i]+1;id=i;}}int ans=0;for (int i=3;i<len-2;i+=2) ans=max(ans,i-front[i]+back[i]-i);printf("%d",ans);return 0;
}