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. 前端 javascript 数据类型 字典

    定义字典 a = {"k1":"v1","k2":"v2",}; Object {k1: "v1", ...

  2. 汇编的WEAK关键字

    一般来说,这个关键字使用在IMPORT和EXPORT这两个声明段. ////////////////////////////////////////////////////////////////// ...

  3. 解决Centos关闭You have new mail in /var/spool/mail/root提示(转)

    今天查看内存的时候 出现一天奇怪的提示 You have new mail in /var/spool/mail/root 有的时候每敲一下回车,就出来You have new mail in /va ...

  4. jmeter接口测试实战

    请求方法:get/post 接口请求地址:http://172.22.24.26:8080/fundhouse/external/getdata?name=xxxx &fund_udid=35 ...

  5. html5的常用函数

    required       验证非空 readonly      文本只读 video          视频播放标签 Ogg            带有 Theora 视频编码和 Vorbis 音 ...

  6. SVN无法Cleanup

    错误如下: 原因:svn提交遇到恶心的问题,可能是因为上次cleanup中断后,进入死循环了 解决方法:清空svn的队列 1.下载Sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc ...

  7. maven tomcat7-maven-plugin配置及背景

    背景: 在研发阶段,想让一个服务通过tomcat启动起来有很多的方法,常用的idea都有这样的支持,那么如果我们没有tomcat,能不能让服务通过tomcat启动起来呢?maven就提供了这样的支持. ...

  8. scp无密码访问scp -i

    网上搜索,可以发现大概有以下情况 1.使用expect交互 2.sshpass 但因为他们都需要安装第三方的软件,不适合我们这样的场景,我们很多时间是绝不允许安装其他软件在客户机上的. 我的场景是在本 ...

  9. c++之旅:类型的强制转换

    类型强制转换 在编程的时候我们经常遇到类型的强制转换,C++为此提供了更安全的转换方式,在编程中我们更多的应该采用C++提供的类型转换方式 基本类型转换 基本类型转换用的最多,一般将高精度转换为低精度 ...

  10. MySql中concat函数的用法(链接字符串)

    MySQL中concat函数使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. 注意:如果所有参数均为非二进制字符串 ...