2084: [Poi2010]Antisymmetry hash+二分

hash一下枚举中点然后二分最大可行长度就好了。

#include<bits/stdc++.h>
#define M 1000000007
#define ll long long 
ll ans;
int n;
ll hash1[500005],hash2[500005],f[500005];
char s[500005];
using namespace std;
inline int hash(int ff,int l,int r)
{
    ll t;
    if (!ff) t=hash1[r]-hash1[l-1]*f[r-l+1];
    else t=hash2[l]-hash2[r+1]*f[r-l+1];
    t=(t%M+M)%M;
    return t;
}
inline void solve(int x)
{
    int l=1,r=min(x,n-x);
    while (l<=r)
    {
        int mid=l+r>>1;
        if (hash(0,x-mid+1,x)==hash(1,x+1,x+mid)) l=mid+1; else r=mid-1;
    }
    ans+=r;
}
int main()
{
    scanf("%d",&n);
    f[0]=1;
    for (int i=1;i<=n;i++)f[i]=f[i-1]*233%M;
    scanf("%s",s+1);
    for (int i=1;i<=n;i++) hash1[i]=(hash1[i-1]*233+s[i]-'0')%M;
    for (int i=n;i;i--) hash2[i]=(hash2[i+1]*233+((s[i]-'0')^1))%M;
    for (int i=1;i<=n;i++) solve(i);
    cout << ans;
    return 0;
}

你可能感兴趣的:(2084: [Poi2010]Antisymmetry hash+二分)