https://vjudge.net/problem/UVALive-6893

题意:

给出一个小矩阵和大矩阵,在大矩阵中能找到相同的小矩阵。

思路:

矩阵Hash,先对小矩阵计算出它的Hash值,然后处理大矩阵,计算出每个子矩阵的Hash值,然后和小矩阵的Hash值比较是否相等。

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
using namespace std; typedef unsigned long long ULL; const ULL Base1 = ;
const ULL Base2 = ; int test,n,m,x,y;
ULL ans;
char s[][],a[][];
ULL hash;
ULL temp[][],Temp[][]; ULL Gethash()
{
ULL c,sum = ;
for(int i = ; i < x; ++i)
{
c = ;
for(int j = ; j < y; ++j)
{
c = c*Base1 + a[i][j];
}
sum = sum * Base2 + c;
}
return sum;
} //计算出每个子矩阵的Hash值,并比较Hash值是否相等
void GetAns()
{
ULL t=, c;
for(int i = ; i < y; ++i) t *= Base1;
for(int i = ; i < n; ++i)
{
c = ;
for(int j = ; j < y; ++j) c = c*Base1 + s[i][j];
temp[i][y-] = c;
for(int j = y; j < m; ++j)
{
temp[i][j]=temp[i][j-]*Base1-s[i][j-y]*t+s[i][j];
}
} t = ;
for(int i = ; i < x; ++i) t *= Base2;
for(int i = y-; i < m; ++i)
{
c = ;
for(int j = ; j < x; ++j) c = c*Base2 + temp[j][i];
Temp[x-][i] = c;
if(c == hash) ans++;
for(int j = x; j < n; ++j)
{
Temp[j][i] = Temp[j-][i]*Base2 - temp[j-x][i] * t + temp[j][i];
if(Temp[j][i]== hash) ans++;
}
}
} int main()
{
//freopen("D:\\input.txt","r",stdin);
while(scanf("%d%d%d%d",&x,&y,&n,&m)!= EOF)
{
for(int i = ; i < x; ++i) scanf("%s",a[i]);
for(int i = ; i < n; ++i) scanf("%s",s[i]);
hash = Gethash();
ans = ;
GetAns();
printf("%llu\n",ans);
}
return ;
}

LA 6893 The Big Painting(矩阵Hash)的更多相关文章

  1. 矩阵hash + KMP - UVA 12886 The Big Painting

    The Big Painting Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=88791 M ...

  2. [poj] 3690 Constellations || 矩阵hash

    原题 在大矩阵里找有几个小矩阵出现过,多组数据 将t个矩阵hash值放入multiset,再把大矩阵中每个hash值从multiset里扔出去,这样最后剩在multiset里的值就是没有找到的小矩阵, ...

  3. LA 6893 矩阵HASH (模板)

    #include<stdio.h> #include<string.h> typedef unsigned long long ULL; ; ; int test,n,m,x, ...

  4. UVALive 6893 The Big Painting hash

    The Big Painting 题目连接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/J Descri ...

  5. 牛客网训练1--------矩阵 (二份+二维矩阵hash)

    不懂hash的话:https://www.cnblogs.com/ALINGMAOMAO/p/10345850.html 思路:对于一个大矩阵的每一个子矩阵都对应着一个hash值k, 当k出现2次以上 ...

  6. UVALive - 6893 The Big Painting 字符串哈希

    题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...

  7. liberOJ #6173. Samjia 和矩阵 hash+后缀数组

    #6173. Samjia 和矩阵 题目链接  : 点这里 题目描述 给你一个只包含大写字母的矩阵,求有多少本质不同的子矩阵. 输入格式 第一行包含两个整数 nnn , mmm ,表示矩阵 nnn 行 ...

  8. BZOJ.2462.[BeiJing2011]矩阵模板(二维Hash)

    题目链接 序列上的Hash和前缀和差不多,二维Hash也和二维前缀和差不多了. 预处理大矩阵所有r*c的小矩阵hash值,再对询问的矩阵Hash. 类比于序列上\(s[r]-s[l-1]*pow[r- ...

  9. 「学习笔记」字符串基础:Hash,KMP与Trie

    「学习笔记」字符串基础:Hash,KMP与Trie 点击查看目录 目录 「学习笔记」字符串基础:Hash,KMP与Trie Hash 算法 代码 KMP 算法 前置知识:\(\text{Border} ...

随机推荐

  1. Zabbix监控基础

    1. 安装前准备 setenforce 0 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config ...

  2. linux中的周期调度器

    2017-06-27 上篇文章简要介绍了Linux进程调度,以及结合源代码窥探了下CFS的调度实例.但是没有深入内部区分析调度下面的操作,比如就绪队列的维护以及进程时间的更新等.本节就这些问题做深入讨 ...

  3. javascript 之 valueOf

    var m = { i:10, toString:function () { console.log('toString'); return this.i; }, valueOf:function ( ...

  4. HTML容易遗忘内容(二)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...

  5. How to store scaling parameters for later use

    you can use sklearn's built-in tool: from sklearn.externals import joblib scaler_filename = "sc ...

  6. python高级之scrapy-redis

    目录: scrapy-redis组件 scrapy-redis配置示例 一.scrapy-redis组件 1.scrapy-redis简介: scrapy-redis是一个基于redis的scrapy ...

  7. @JsonFormat与@DateTimeFormat注解的使用

    背景:从数据库获取时间传到前端进行展示的时候,我们有时候可能无法得到一个满意的时间格式的时间日期,在数据库中显示的是正确的时间格式,获取出来却变成了很丑的时间戳,@JsonFormat注解很好的解决了 ...

  8. Linux服务器access_log日志分析及配置详解(二)

    默认nginx / Linux日志在哪个文件夹? 一般在 xxx.xxx.xxxx.com/home/admin 路径下面的error.log文件和access.log文件error_log logs ...

  9. 工作笔记——dom属性巧用

    1.获取验证码 dom: <span class="btn btn-primary">获取验证码</span> js: /** *@Author Mona ...

  10. Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据

    233 Matrix Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Descript ...