模板—字符串—Manacher

模板—字符串—Manacher

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 11000010
int len[N<<1],n,ans; char str[N],s[N<<1]; long long sum;
namespace Manacher
{
	int init(char *str)
	{
		int n=strlen(str);
		for(int i=1,j=0;i<=2*n;j++,i+=2)
			s[i]='#',s[i+1]=str[j];
		s[0]='$',s[2*n+1]='#',s[2*n+2]='@',s[2*n+3]='\0';
		return 2*n+1;
	}
	void manacher(int l,int r)
	{
		int mx=0,p=0;
		for(int i=l;i<=r;i++)
		{
			if(mx>i) len[i]=min(mx-i,len[2*p-i]);
			else len[i]=1;
			while(s[i-len[i]]==s[i+len[i]]) len[i]++;
			if(len[i]+i>mx) mx=len[i]+i,p=i;
		}
	}
}
using namespace Manacher;
int main()
{
	scanf("%s",str),n=init(str),manacher(1,n);
	for(int i=1;i<=n;i++) ans=max(ans,len[i]/2*2-(i%2==0)),sum+=len[i]/2;
	printf("%d %lld\n",ans,sum);
}

  

posted @ 2019-04-04 19:18  Yang1208  阅读(148)  评论(0编辑  收藏  举报