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

8 篇文章 0 订阅

题意:

求多少个二维的回文串。

题解:

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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#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<n;i++)
        for(int j=1;j<m;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);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值