LA 6893 The Big Painting(矩阵Hash)
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)的更多相关文章
- 矩阵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 ...
- [poj] 3690 Constellations || 矩阵hash
原题 在大矩阵里找有几个小矩阵出现过,多组数据 将t个矩阵hash值放入multiset,再把大矩阵中每个hash值从multiset里扔出去,这样最后剩在multiset里的值就是没有找到的小矩阵, ...
- LA 6893 矩阵HASH (模板)
#include<stdio.h> #include<string.h> typedef unsigned long long ULL; ; ; int test,n,m,x, ...
- UVALive 6893 The Big Painting hash
The Big Painting 题目连接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/J Descri ...
- 牛客网训练1--------矩阵 (二份+二维矩阵hash)
不懂hash的话:https://www.cnblogs.com/ALINGMAOMAO/p/10345850.html 思路:对于一个大矩阵的每一个子矩阵都对应着一个hash值k, 当k出现2次以上 ...
- UVALive - 6893 The Big Painting 字符串哈希
题目链接: http://acm.hust.edu.cn/vjudge/problem/129730 The Big Painting Time Limit: 5000MS 题意 给你一个模板串和待匹 ...
- liberOJ #6173. Samjia 和矩阵 hash+后缀数组
#6173. Samjia 和矩阵 题目链接 : 点这里 题目描述 给你一个只包含大写字母的矩阵,求有多少本质不同的子矩阵. 输入格式 第一行包含两个整数 nnn , mmm ,表示矩阵 nnn 行 ...
- BZOJ.2462.[BeiJing2011]矩阵模板(二维Hash)
题目链接 序列上的Hash和前缀和差不多,二维Hash也和二维前缀和差不多了. 预处理大矩阵所有r*c的小矩阵hash值,再对询问的矩阵Hash. 类比于序列上\(s[r]-s[l-1]*pow[r- ...
- 「学习笔记」字符串基础:Hash,KMP与Trie
「学习笔记」字符串基础:Hash,KMP与Trie 点击查看目录 目录 「学习笔记」字符串基础:Hash,KMP与Trie Hash 算法 代码 KMP 算法 前置知识:\(\text{Border} ...
随机推荐
- SqlServer SqlBulkCopy批量插入 -- 多张表同时插入(事务)
这段时间在解决一个多个表需要同时插入大量数据的问题,于是在网上找了下,查到说用SqlBulkCopy效率很高,实验后确实很快,10万条数据只要4秒钟,用ef要用40秒.但是我的还需两张表同时插入,且需 ...
- Apache mahout 源码阅读笔记--DataModel之FileDataModel
要做推荐,用户行为数据是基础. 用户行为数据有哪些字段呢? mahout的DataModel支持,用户ID,ItemID是必须的,偏好值(用户对当前Item的评分),时间戳 这四个字段 {@code ...
- app后端设计(0)--总目录(转)
原文:http://blog.csdn.net/newjueqi/article/details/19003775 做了接近两年app相关的系统架构,api设计,先后在两个创业公司中工作,经历过手机网 ...
- 浅谈pipreqs组件(自动生成需要导入的模块信息)
简介 pipreqs的作用 一起开发项目的时候总是要搭建环境和部署环境的,这个时候必须得有个python第三方包的list,一般都叫做requirements.txt. 如果一个项目使用时virtua ...
- django 模板语言之 filter 自定义模板
可以自己写python函数放在模板语言里用 这种方法是django里面的 filter {{ item.event_start|date:"Y-m-d H:i:s"}} {{ bi ...
- Spring boot 打包瘦身方法
背景 随着spring boot 的流行.越来越多的来发着选择使用spring boot 来发 web 应用. 不同于传统的 web 应用 需要 war 包来发布应用. spring boot 应用可 ...
- 转 Merkle Tree(默克尔树)算法解析
Merkle Tree概念 Merkle Tree,通常也被称作Hash Tree,顾名思义,就是存储hash值的一棵树.Merkle树的叶子是数据块(例如,文件或者文件的集合)的hash值.非叶节 ...
- STL make_heap push_heap pop_heap sort_heap
make_heap: default (1) template <class RandomAccessIterator> void make_heap (RandomAccessItera ...
- Writing a device driver for Windows
Writing a device driver for Windows In order to write a device driver for windows, one needs ...
- Get started on your own KD 8 custom colorway
The 2009 Summer time Nike Basketball revealed the Cheap KD 8 and revealed three MVP-inspired colors ...