POJ 3690 Constellations + Gym - 100783J The Big Painting 二维字符串hash

这次组队赛也出现了同样的题目,然而不会做。所以来学习一波新姿势。
二维字符串hash的原理就是先把行横着做普通的一位字符串hash,然后每一行都有一个hash值,再竖着将行与行的hash值再hash一次。原理并不复杂。然后注意横着和竖着hash取两个不同的大质数就可以了。
x1 , 当做横着hash的质数, x2 当做竖着hash的质数。
对于这题来说。
r[i][j] 表示横着hash的时候,第i行从 jq+1...j 的hash值
c[j][i] 则是从左上角的(j-p+1,i-q+1) 到(j,i)的二维矩阵的hash值

r[i][j]=r[i][j1]x1+s[i][j]s[i][jq]xq1

c[j][i]=c[j1][i]x2+r[j][i]r[jp][i]xp2

然后给你的模式矩阵可能会相同,所以使用multiset,可以把hash值相同的全部去掉。
最后用总数减去set中剩余的就是匹配上的数量。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>

using namespace std;
typedef unsigned long long ull;
const int maxn = 1010;
ull r[maxn][maxn],c[maxn][maxn];
char s[maxn][maxn],tt[maxn][maxn];
int n,m,p,q,t;
const ull x1 = 100000007;
const ull x2 = 1000000007;
multiset<ull> mp;
ull gethash()
{
    ull tmp=0,ttmp=0;
    for (int i=0;i<p;i++)
    {
        tmp=0;
        for (int j=0;j<q;j++)
            tmp = tmp*x1 + tt[i][j];
        ttmp = ttmp * x2 + tmp;
    }
    mp.insert(ttmp);
}

void solve()
{
    ull o = 1,tmp;
    for (int i=0;i<q;i++) o *= x1;
    for (int i=0;i<n;i++)
    {
        tmp = 0;
        for (int j=0;j<q;j++) tmp = tmp*x1 + s[i][j];
        r[i][q-1] = tmp;
        for (int j=q;j<m;j++) r[i][j] = r[i][j-1] * x1 + s[i][j] - s[i][j-q]*o;
    }
    o = 1;
    for (int i=0;i<p;i++) o *= x2;
    for (int i=q-1;i<m;i++)
    {
        tmp = 0;
        for (int j=0;j<p;j++) tmp = tmp*x2 + r[j][i];
        c[p-1][i] = tmp;
        mp.erase(tmp);
        for (int j=p;j<n;j++)
        {
            c[j][i] = c[j-1][i]*x2 + r[j][i] - r[j-p][i]*o;
            mp.erase(c[j][i]);
        }
    }
}


int main()
{
    int ttt=0;
    while (scanf("%d%d%d%d%d",&n,&m,&t,&p,&q), n||m||t||p||q)
    {
        mp.clear();
        for (int i=0;i<n;i++) scanf("%s",s[i]);
        for (int i=0;i<t;i++)
        {
            memset(tt,0,sizeof tt);
            for (int j=0;j<p;j++) scanf("%s",tt[j]);
            gethash();
        }
        solve();
        printf("Case %d: %d\n",++ttt,t-mp.size());
    }
    return 0;
}

然后再附赠一题这次组队赛的题,如果知道了这种做法的话这题就是个裸题了,复杂度只有 O(n2)
GYM-100783J
代码几乎是一样的,就不放了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值