bzoj 1414: [ZJOI2009]对称的正方形

题意:

求多少个二维的回文串。

题解:

向四个方向hash,枚举中间点,分奇偶讨论,二分边长。
unsigned int AC unsigned long long WA是什么鬼。
code:

#include
#include
#include
#include
#define LL unsigned int
using namespace std;
const LL base1=233,base2=2333;
LL pre1[1001],pre2[1001];
int n,m;
LL hash[4][1001][1001];
int a[1010][1001];
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
LL get0(int x,int y,int x1,int y1){return hash[0][x1][y1]-hash[0][x1][y-1]*pre1[y1-y+1]-hash[0][x-1][y1]*pre2[x1-x+1]+hash[0][x-1][y-1]*pre2[x1-x+1]*pre1[y1-y+1];}
LL get2(int x,int y,int x1,int y1){return hash[2][x][y]-hash[2][x1+1][y]*pre2[x1-x+1]-hash[2][x][y1+1]*pre1[y1-y+1]+hash[2][x1+1][y1+1]*pre2[x1-x+1]*pre1[y1-y+1];}
LL get1(int x,int y,int x1,int y1){return hash[1][x1][y]-hash[1][x1][y1+1]*pre1[y1-y+1]-hash[1][x-1][y]*pre2[x1-x+1]+hash[1][x-1][y1+1]*pre2[x1-x+1]*pre1[y1-y+1];}
LL get3(int x,int y,int x1,int y1){return hash[3][x][y1]-hash[3][x][y-1]*pre1[y1-y+1]-hash[3][x1+1][y1]*pre2[x1-x+1]+hash[3][x1+1][y-1]*pre2[x1-x+1]*pre1[y1-y+1];}
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) a[i][j]=read();
    pre1[0]=pre2[0]=1;
    for(int i=1;i<=1000;i++) pre1[i]=pre1[i-1]*base1,pre2[i]=pre2[i-1]*base2;
    for(int j=1;j<=m;j++)
        for(int i=1;i<=n;i++) hash[0][i][j]=hash[0][i-1][j]*base2+a[i][j];
    for(int j=1;j<=m;j++)
        for(int i=1;i<=n;i++) hash[1][i][j]=hash[1][i-1][j]*base2+a[i][j];
    for(int j=1;j<=m;j++)
        for(int i=n;i>=1;i--) hash[2][i][j]=hash[2][i+1][j]*base2+a[i][j];
    for(int j=1;j<=m;j++)
        for(int i=n;i>=1;i--) hash[3][i][j]=hash[3][i+1][j]*base2+a[i][j];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) hash[0][i][j]+=hash[0][i][j-1]*base1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) hash[3][i][j]+=hash[3][i][j-1]*base1;
    for(int i=1;i<=n;i++)
        for(int j=m;j>=1;j--) hash[1][i][j]+=hash[1][i][j+1]*base1;
    for(int i=1;i<=n;i++)
        for(int j=m;j>=1;j--) hash[2][i][j]+=hash[2][i][j+1]*base1;
    int ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            int l=1,r=min(min(i,n-i+1),min(j,m-j+1)),t=1;
            while(l<=r)
            {
                int mid=(l+r)/2;
                LL n0=get0(i-mid+1,j-mid+1,i,j);
                LL n1=get1(i-mid+1,j,i,j+mid-1);
                LL n2=get2(i,j,i+mid-1,j+mid-1);
                LL n3=get3(i,j-mid+1,i+mid-1,j);
                if(n0==n1&&n1==n2&&n2==n3) l=mid+1,t=mid;
                else r=mid-1;
            }
            ans+=t;
        }
    for(int i=1;i
        for(int j=1;j
        {
            int l=1,r=min(min(i,n-i),min(j,m-j)),t=0;
            while(l<=r)
            {
                int mid=(l+r)/2;
                LL n0=get0(i-mid+1,j-mid+1,i,j);
                LL n1=get1(i-mid+1,j+1,i,j+mid);
                LL n2=get2(i+1,j+1,i+mid,j+mid);
                LL n3=get3(i+1,j-mid+1,i+mid,j);
                if(n0==n1&&n1==n2&&n2==n3) l=mid+1,t=mid;
                else r=mid-1;
            }
            ans+=t;
        }
    printf("%d",ans);
}

你可能感兴趣的:(hash,二分)