跳到正文

剪贴板

剪贴板 pfu56osx

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int n,p=13331;
char s[500010],t[500010];
int op[500010],f[500010],g[500010];
int ans;
void ef(int x)
{
	int l=0,r=min(x,n-x);
	while(l<r)
	{
		int m=(l+r+1)>>1;
		int h1=f[x]-f[x-m]*op[m],h2=g[x+1]-g[x+1+m]*op[m];
		if(h1==h2) l=m;
		else r=m-1;
	}
	ans+=l;
}
int read()
{
	char c=getchar();int x=0;
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}
signed main()
{
	n=read();scanf("%s",s+1);op[0]=1;
	for(int i=1;i<=n;i++) t[i]=s[i]^1;
	for(int i=1;i<=n;i++) op[i]=op[i-1]*p;
	for(int i=1;i<=n;i++) f[i]=f[i-1]*p+(s[i]-'0');
	for(int i=n;i>=1;i--) g[i]=g[i+1]*p+(t[i]-'0');
	for(int i=1;i<n;i++)
	{
		if(s[i]==s[i+1])continue;
	    ef(i);
	}
	cout<<ans;
	return 0;
}