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. 观 浅谈HTTP中Get与Post的区别

    看完解决了一些模糊不清的.错误的理解.

  2. ubuntu14下创建软件的快捷启动方式

    下载软件,使用softname/bin/softname.sh即可启动,但是很麻烦,每次都要打开terminal 为了方便,我们需要创建desktop文件指向这个启动软件的shell文件(以创建Pyc ...

  3. Flask上下文管理

    一.一些python的知识 1.偏函数 def add(x, y, z): print(x + y + z) # 原本的写法:x,y,z可以传任意数字 add(1,2,3) # 如果我要实现一个功能, ...

  4. SpringBoot安装和创建简单的Web应用

    SpringBoot安装 方式一: Eclipese->Help->Eclipse Marketplace ->Finde STS -> Install 注意:安装过程中挺慢, ...

  5. 使用Django和Python创建Json response

    版权声明:本文为博主原创文章,欢迎转载. https://blog.csdn.net/fengyu09/article/details/30785101 使用jquery的.post提交,并期望得到多 ...

  6. windows server 2008 r2 IIS 6 元数据库与IIS 6 配置的兼容性 解决方案

    1 菜单-管理工具-服务器管理 2 添加角色服务 3 选中 IIS6 管理兼容性 4 安装完成 5 在IIS上新建一个相应端口的网站 6重新加载项目,OK

  7. vue 动画过渡

    一.过渡(动画) 1. 简介 Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果,本质上还是使用CSS3动画:transition.animation 2. 基本用法 1.使用t ...

  8. java通过url抓取网页数据

    在很多行业中,要对行业数据进行分类汇总,及时分析行业数据,对于公司未来的发展,有很好的参照和横向对比.所以,在实际工作,我们可能要遇到数据采集这个概念,数据采集的最终目的就是要获得数据,提取有用的数据 ...

  9. SQL Server语句创建数据库和表——并设置主外键关系

    简单的创建数据库的 SQL 语句: use master go if exists(select * from sysdatabases where name='Test') begin select ...

  10. PKU 3318 Matrix Multiplication(随机化算法||状态压缩)

    题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...