LA 6893 矩阵HASH (模板)
#include<stdio.h>
#include<string.h>
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,d = ;
for(int i = ; i < x; ++i) {
c = ;
for(int j = ; j < y; ++j)
{
c = c*Base1 + a[i][j];
}
d = d * Base2 + c;
}
return d;
} void GetAns() {//把矩阵中的hash算出来,每个子矩阵的。
ULL t,a; // 算的时候与目标hash匹配判断一下....
t = ;
for(int i = ; i < y; ++i) t *= Base1;
for(int i = ; i < n; ++i) {
a = ;
for(int j = ; j < y; ++j) a = a*Base1 + s[i][j];
temp[i][y-] = a;
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) {
a = ;
for(int j = ; j < x; ++j) a = a*Base2 + temp[j][i];
Temp[x-][i] = a;
if(a == 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()
{
while(scanf("%d%d%d%d",&x,&y,&n,&m)!= EOF) {
//scanf("%d%d",&n,&m);
for(int i = ; i < x; ++i) scanf("%s",a[i]);
//scanf("%d%d",&n,&m);
for(int i = ; i < n; ++i) scanf("%s",s[i]);
hash = Gethash();
ans = ;
GetAns();
printf("%llu\n",ans);
}
return ;
}
LA 6893 矩阵HASH (模板)的更多相关文章
- LA 6893 The Big Painting(矩阵Hash)
https://vjudge.net/problem/UVALive-6893 题意: 给出一个小矩阵和大矩阵,在大矩阵中能找到相同的小矩阵. 思路: 矩阵Hash,先对小矩阵计算出它的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里的值就是没有找到的小矩阵, ...
- 线段树、KMP、HASH模板
线段树 #include<cstdio> using namespace std; int n,p,a,b,m,x,y,ans; struct node { int l,r,w,f; }t ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
- 牛客网训练1--------矩阵 (二份+二维矩阵hash)
不懂hash的话:https://www.cnblogs.com/ALINGMAOMAO/p/10345850.html 思路:对于一个大矩阵的每一个子矩阵都对应着一个hash值k, 当k出现2次以上 ...
- [luogu P1962] 斐波那契数列(带快速幂矩阵乘法模板)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- LA 4670 (AC自动机 模板题) Dominating Patterns
AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目.=_=|| AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配. 因为 ...
- LA 3704 (矩阵快速幂 循环矩阵) Cellular Automaton
将这n个格子看做一个向量,每次操作都是一次线性组合,即vn+1 = Avn,所求答案为Akv0 A是一个n*n的矩阵,比如当n=5,d=1的时候: 不难发现,A是个循环矩阵,也就是将某一行所有元素统一 ...
随机推荐
- python练习实例2--------构造数据类型
有如下数据类型 cars = ['鲁A32444', '鲁B12333', '京B8989M', '黑C49678', '黑C46555', '沪B25041', '黑C34567'] locatio ...
- [Linux] [JNI]
使用 javah 生成头文件后, 编写c代码来实现其中声明的函数, 本文主要解决以下问题: (1) 如何生成动态链接库文件 使用如下格式的 gcc 命令可以将 C文件 编译为 .so 文件, 对于其依 ...
- gethostbyname和gethostbyaddr
一.gethostbyname函数原型 #include <netdb.h> struct hostent *gethostbyname(const char *ghostname); 返 ...
- 俄罗斯方块部分功能(Java)
package OO.day01; public class TetrisCell { int totalRow = 20; int totalcol = 10; //定义横宽 int row; in ...
- memset赋值
比较神奇的事情 可能和二进制有关系吧 #include<bits/stdc++.h> using namespace std; ]; int main(){ memset(f,,sizeo ...
- pycharm活动模板
1.复制模板内容 2.文件设置: 点击+号:
- [C++]PAT乙级1008.数组元素循环右移问题 (20/20)
/* 1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数, ...
- Java -cp 命令行引用多个jar包的简单写法(Windows、Linux
1.Windows下用法 在Windows上,可以使用 用法:java your-jar-lib-folder/* your-main-class your-jar-lib-folder为存放一堆ja ...
- Leetcode#561. Array Partition I(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- 小程序view排版
<view class="bc"> <view> <text bc_text>demo</text> </view> & ...