【bzoj2351】[BeiJing2011]Matrix hash表+双hash

Description

给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过。
所谓01矩阵,就是矩阵中所有元素不是0就是1。

Input

输入文件的第一行为M、N、A、B,参见题目描述。
接下来M行,每行N个字符,非0即1,描述原矩阵。
接下来一行为你要处理的询问数Q。
接下来Q个矩阵,一共Q*A行,每行B个字符,描述Q个01矩阵。

Output

你需要输出Q行,每行为0或者1,表示这个矩阵是否出现过,0表示没有出现过,1表示出现过。

Sample Input

3 3 2 2

111

000

111

3

11

00

11

11

00

11

Sample Output

1

0

1

HINT

对于100%的实际测试数据,M、N ≤ 1000,Q = 1000

对于40%的数据,A = 1。

对于80%的数据,A ≤ 10。

对于100%的数据,A ≤ 100。

Source


写个hash表练习
双hash是我闲的蛋疼
这个A不了bzoj2462,因为卡常了…模多了就是不好啊

所以那个题直接输出1就行了233

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long LL;
const int SZ = 2010;
const int SZ2 = 1000010;
const int base1 = 13331;
const int base2 = 1333331;
const int mod1 = 1000003;
const int mod2 = 998244353;

char a[SZ][SZ];

struct pair{
    int x,y;
}h_sum[SZ][SZ],mi1[SZ],mi2[SZ],hh[SZ][SZ];

struct hashlist{
    int x;
    int cnt;
    hashlist *nxt;
}*h[SZ2],T[SZ2];

int Tcnt = 0;

hashlist* newnode(int x)
{
    hashlist *k = T + (Tcnt ++);
    k -> x = x;
    k -> cnt = 1;
    k -> nxt = NULL;
    return k;
}

void insert(int pos,int x)
{
    pos = (pos % mod1 + mod1) % mod1;
    x = (x % mod2 + mod2) % mod2;
    if(!h[pos]) h[pos] = newnode(x);
    else for(hashlist *p = h[pos];p;p = p -> nxt)
    {
        if(p -> x == x)
        {
            p -> cnt ++;
            break;
        }
        if(!p -> nxt) 
        {
            p -> nxt = newnode(x);
            break;
        }
    }
}

bool find(int x,int y)
{
    x = (x % mod1 + mod1) % mod1;
    y = (y % mod2 + mod2) % mod2;
    for(hashlist *p = h[x];p;p = p -> nxt)
        if(p -> x == y) return true;
    return false;
}

char s[SZ][SZ];

int main()
{
    int n,m,r,c;
    scanf("%d%d%d%d",&n,&m,&r,&c);
    for(int i = 1;i <= n;i ++)
        scanf("%s",a[i] + 1);
    mi1[0].x = mi1[0].y = 1;
    mi2[0].x = mi2[0].y = 1;
    for(int i = 1;i <= 1010;i ++)
    {
        mi1[i].x = (LL)mi1[i - 1].x * base1 % mod1;
        mi2[i].x = (LL)mi2[i - 1].x * base2 % mod1;
        mi1[i].y = (LL)mi1[i - 1].y * base1 % mod2;
        mi2[i].y = (LL)mi2[i - 1].y * base2 % mod2;
    }
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
        {
            h_sum[i][j].x = ((LL)h_sum[i][j - 1].x * base1 % mod1 + a[i][j] - '0' + 1) % mod1;
            h_sum[i][j].y = ((LL)h_sum[i][j - 1].y * base1 % mod2 + a[i][j] - '0' + 1) % mod2;
        }
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
        {
            h_sum[i][j].x = ((LL)h_sum[i - 1][j].x * base2 % mod1 + h_sum[i][j].x) % mod1;
            h_sum[i][j].y = ((LL)h_sum[i - 1][j].y * base2 % mod2 + h_sum[i][j].y) % mod2;
        }

    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
        {
            int x = h_sum[i][j].x; 
            x = (x - (LL)h_sum[i - r][j].x * mi2[r].x % mod1) % mod1; 
            x = (x - (LL)h_sum[i][j - c].x * mi1[c].x % mod1) % mod1; 
            x = (x + (LL)h_sum[i - r][j - c].x * mi1[c].x % mod1 * mi2[r].x % mod1) % mod1; 
            int y = h_sum[i][j].y; 
            y = (y - (LL)h_sum[i - r][j].y * mi2[r].y % mod2) % mod2; 
            y = (y - (LL)h_sum[i][j - c].y * mi1[c].y % mod2) % mod2; 
            y = (y + (LL)h_sum[i - r][j - c].y * mi1[c].y % mod2 * mi2[r].y % mod2) % mod2;             
            insert(x,y);
        }   
    int q;
    scanf("%d",&q);
    while(q --)
    {
        for(int i = 1;i <= r;i ++)
            scanf("%s",s[i] + 1);
        for(int i = 1;i <= r;i ++)
            for(int j = 1;j <= c;j ++)
            {
                hh[i][j].x = ((LL)hh[i][j - 1].x * base1 % mod1 + s[i][j] - '0' + 1) % mod1;
                hh[i][j].y = ((LL)hh[i][j - 1].y * base1 % mod2 + s[i][j] - '0' + 1) % mod2;
            }
        for(int i = 1;i <= r;i ++)
            for(int j = 1;j <= c;j ++)
            {
                hh[i][j].x = ((LL)hh[i - 1][j].x * base2 % mod1 + hh[i][j].x) % mod1;
                hh[i][j].y = ((LL)hh[i - 1][j].y * base2 % mod2 + hh[i][j].y) % mod2;
            }       
        puts(find(hh[r][c].x,hh[r][c].y) ? "1" : "0");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值