BZOJ2462

BZOJ 2462 (二维hash学习)
  • 二维hash

    创建两个种子 b a s e 1 , b a s e 2 base1,base2 base1,base2 创建一个 无符号 64 64 64 位数组 h a s h a hasha hasha

    先对每一行做前缀 h a s h hash hash

    for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++)
    			hasha[i][j] = hasha[i][j - 1] * base1 + a[i][j];
    

    再做 1 , 1 ∼ i , j 1,1 \sim i,j 1,1i,j h a s h hash hash

    for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++)
    			hasha[i][j] += hasha[i - 1][j] * base2;
    

    二维 h a s h hash hash = = = 当前行的前缀到 j j j h a s h hash hash + + + i − 1 i - 1 i1 h a s h hash hash

    查询:

    hasha[i][j] - hasha[i][j - y]*pow1[y] - hasha[i - x][j]*pow2[x] + hasha[i - x][j - y]*pow1[y]*pow2[x]
    

    查询的是 i ≥ x i \geq x ix j ≥ y j \geq y jy 时 矩形大小为 x , y x,y x,y h a s h hash hash 值 , h a s h [ i ] [ j ] hash[i][j] hash[i][j] 是右下角坐标二维前缀 h a s h hash hash

  • 题目

  • 代码

    int ans, cas = 0;
    	pow1[0] = pow2[0] = 1;
    	for (int i = 1; i <= 1001; i++) pow1[i] = pow1[i - 1] * base1, pow2[i] = pow2[i - 1] * base2;
    	scanf("%d %d %d %d", &n, &m, &x, &y);
    	v.clear();
    	for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
    	for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++)
    			hasha[i][j] = hasha[i][j - 1] * base1 + a[i][j];
    	for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= m; j++)
    			hasha[i][j] += hasha[i - 1][j] * base2;
    	for (int i = x; i <= n; i++)
    		for (int j = y; j <= m; j++)
    			v.push_back(hasha[i][j] - hasha[i][j - y]*pow1[y] - hasha[i - x][j]*pow2[x] + hasha[i - x][j - y]*pow1[y]*pow2[x]); // 从 x,y开始保证矩形大小不小于x,y
    	sort(v.begin(), v.end());
    	int t;
    	scanf("%d", &t);
    	while (t--)
    	{
    		for (int i = 1; i <= x; i++) scanf("%s", b[i] + 1);
    		for (int i = 1; i <= x; i++)
    			for (int j = 1; j <= y; j++)
    				hashb[i][j] = hashb[i][j - 1] * base1 + b[i][j];
    		for (int i = 1; i <= x; i++)
    			for (int j = 1; j <= y; j++)
    				hashb[i][j] += hashb[i - 1][j] * base2;
    		int pos = lower_bound(v.begin(), v.end(), hashb[x][y]) - v.begin();
    		if (pos == v.size()){
    			puts("0");
    			continue;
    		}
    		if (v[pos] == hashb[x][y])
    			puts("1");
    		else puts("0");
        }
    
  • 题型
    二维 h a s h hash hash

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值