BZOJ2462 二维hash

这题挺裸的 想着是hash做 但是还不会二维的 搜了份题解现场学习了一发

大佬代码

#include<bits/stdc++.h>
#define MAX 1100  
using namespace std;  
const unsigned int BASE1 = 10016957;  
const unsigned int BASE2 = 10016957;  
const int MO = 99999997;  

int m,n,ask_m,ask_n,asks;  
unsigned int hash[MAX][MAX],_hash[MAX][MAX];  
unsigned int pow1[MAX],pow2[MAX];  

bool set[100000000];  

inline unsigned int GetHash()  
{  
    for(int i = 1; i <= ask_m; ++i)  
        for(int j = 1; j <= ask_n; ++j)  
            _hash[i][j] += _hash[i - 1][j] * BASE1;  
    for(int i = 1; i <= ask_m; ++i)  
        for(int j = 1; j <= ask_n; ++j)  
            _hash[i][j] += _hash[i][j - 1] * BASE2;  
    return _hash[ask_m][ask_n];  
}  

int main()  
{  
    cin >> m >> n >> ask_m >> ask_n;  
    for(int i = 1; i <= m; ++i)  
        for(int j = 1; j <= n; ++j)  
            scanf("%1d",&hash[i][j]);  
    pow1[0] = pow2[0] = 1;  
    for(int i = 1; i <= 100; ++i)  
        pow1[i] = pow1[i - 1] * BASE1,pow2[i] = pow2[i - 1] * BASE2;  
    for(int i = 1; i <= m; ++i)  
        for(int j = 1; j <= n; ++j)  
            hash[i][j] += hash[i - 1][j] * BASE1;  
    for(int i = 1; i <= m; ++i)  
        for(int j = 1; j <= n; ++j)  
            hash[i][j] += hash[i][j - 1] * BASE2;  
    for(int i = ask_m; i <= m; ++i)  
        for(int j = ask_n; j <= n; ++j) {  
            unsigned int h = hash[i][j];  
            h -= hash[i - ask_m][j] * pow1[ask_m];  
            h -= hash[i][j - ask_n] * pow2[ask_n];  
            h += hash[i - ask_m][j - ask_n] * pow1[ask_m] * pow2[ask_n];  
            set[h % MO] = true;  
        }  
    for(cin >> asks; asks--;) {  
        for(int i = 1; i <= ask_m; ++i)  
            for(int j = 1; j <= ask_n; ++j)  
                scanf("%1d",&_hash[i][j]);  
        puts(set[GetHash() % MO] ? "1":"0");  
    }  
    return 0;  
}  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值