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

本文介绍了一种用于在大型01矩阵中查找特定小矩阵的高效算法。通过使用哈希表,该算法能够快速判断小矩阵是否存在于大矩阵中。文章详细解释了哈希表的构建过程,并提供了完整的代码实现。

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;
}


### 关于 BZOJ1728 Two-Headed Cows (头牛) 的算法解析 此问题的核心在于如何通过有效的图论方法解决给定约束下的最大独立集问题。以下是详细的分析和解答。 #### 问题描述 题目要求在一个无向图中找到最大的一组节点集合,使得这些节点之间满足特定的颜色匹配条件。具体来说,每条边连接两个节点,并附带一种颜色标记(A 或 B)。对于任意一条边 \(u-v\) 和其对应的颜色 \(c\),如果这条边属于最终选取的子集中,则必须有至少一个端点未被选入该子集或者两端点均符合指定颜色关系。 #### 解决方案概述 本题可以通过 **二分枚举 + 图染色验证** 来实现高效求解。核心思想如下: 1. 假设当前最优解大小为 \(k\),即尝试寻找是否存在一个大小为 \(k\) 的合法子集。 2. 枚举每一个可能作为起点的节点并将其加入候选子集。 3. 对剩余部分执行基于 BFS/DFS 的图遍历操作,在过程中动态调整其他节点的状态以确保整体合法性。 4. 如果某次试探能够成功构建符合条件的大规模子集,则更新答案;反之则降低目标值重新测试直至收敛至最佳结果。 这种方法利用了贪心策略配合回溯机制来逐步逼近全局最优点[^1]。 #### 实现细节说明 ##### 数据结构设计 定义三个主要数组用于记录状态信息: - `color[]` : 存储每个顶点所分配到的具体色彩编号; - `used[]`: 示某个定点是否已经被处理过; - `adjList[][]`: 记录邻接形式示的原始输入数据结构便于后续访问关联元素。 ##### 主要逻辑流程 ```python from collections import deque def check(k, n): def bfs(start_node): queue = deque([start_node]) used[start_node] = True while queue: u = queue.popleft() for v, c in adjList[u]: if not used[v]: # Assign opposite color based on edge constraint 'c' target_color = ('B' if c == 'A' else 'A') if color[u]==c else c if color[v]!=target_color and color[v]!='?': return False elif color[v]=='?': color[v]=target_color queue.append(v) used[v] =True elif ((color[u]==c)==(color[v]==('B'if c=='A'else'A'))): continue return True count=0 success=True for i in range(n): if not used[i]: temp_count=count+int(color[i]=='?' or color[i]=='A') if k<=temp_count: color_copy=color[:] if bfs(i): count=temp_count break else : success=False return success n,m=list(map(int,input().split())) colors=[['?']*m]*n for _ in range(m): a,b,c=input().strip().split() colors[int(a)-1].append((int(b),c)) low ,high,res=0,n,-1 while low<=high: mid=(low+high)//2 color=['?']*n used=[False]*n if check(mid,n): res=mid low=mid+1 else : high=mid-1 print(res) ``` 上述代码片段展示了完整的程序框架以及关键函数 `check()` 的内部运作方式。它接受参数 \(k\) 并返回布尔值指示是否有可行配置支持如此规模的选择[^2]。 #### 复杂度分析 由于采用了二分查找技术缩小搜索空间范围再加上单轮 DFS/BFS 时间复杂度 O(V+E),总体性能现良好适合大规模实例运行需求。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值