题意:在n*m方格中找有几个x*y矩阵。

思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash。之前还在想怎么快速把一个矩阵的hash算出来,然后看到是尺取,也不知道是什么...这应该算是用到了这个思想吧。

要先预处理每行y个的hash(看代码),然后每次算出上面两顶点在第一行的hash值,慢慢往下移。hash看上去就像是在算一个seed位数一样,seed取13那么就是把字符串转化为了一个13位数,这样想可能在移动的时候更容易理解代码的含义。

代码:

#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = + ;
const int seed1 = ;
const int seed2 = ;
const int MOD = ;
const int INF = 0x3f3f3f3f;
char s[maxn][maxn],str[maxn];
ull ha[maxn][maxn];
int T;
int n, m, x, y;
int solve(ull aim){
ull bit = ;
for(int i = ; i <= y - ; i++)
bit = bit * seed1;
for(int i = ; i <= n; i++){ //预处理每一行的hash
ull sum = ;
for(int j = ; j <= y - ; j++){
sum = sum * seed1 + s[i][j];
}
for(int j = y; j <= m; j++){
sum = sum * seed1 + s[i][j];
ha[i][j - y + ] = sum;
sum -= bit * s[i][j - y + ];
}
}
int ans = ;
bit = ;
for(int i = ; i <= x - ; i++)
bit = bit * seed2;
for(int j = ; j <= m - y + ; j++){
ull sum = ;
for(int i = ; i <= x - ; i++){
sum = sum * seed2 + ha[i][j];
}
for(int i = x; i <= n; i++){
sum = sum * seed2 + ha[i][j];
if(sum == aim) ans++;
sum -= bit * ha[i - x + ][j];
}
}
return ans; }
int main(){
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%s", s[i] + );
scanf("%d%d", &x, &y);
ull temp, aim = ; //先对x*y二维hash
for(int i = ; i <= x; i++){
scanf("%s", str + );
temp = ;
for(int j = ; j <= y; j++){
temp = temp * seed1 + str[j];
}
aim = aim * seed2 + temp;
}
printf("%d\n", solve(aim));
}
return ;
}

最新文章

  1. JAVABEAN连接各数据库
  2. C#面向对象中类的静态成员与非静态成员的区别
  3. What does addScalar do?
  4. Frameset标签
  5. &lt;转载&gt;CSS解决图片过大撑破DIV的方法
  6. hdu 2838 Cow Sorting(树状数组)
  7. 简单使用JSON,JavaScript中创建 JSON 对象(一)
  8. MC34063+MOSFET扩流 12V-5V 折腾出了高效率电路(转)
  9. # hadoop入门第六篇:Hive实例
  10. 建立自己的git服务器
  11. Hbase出现ERROR: Can&#39;t get master address from ZooKeeper; znode data == null解决办法
  12. 什么是ZooKeeper?
  13. 小程序中使用ECharts 异步加载数据
  14. Android 系统服务的获取与创建
  15. 实现对Java配置文件Properties的读取、写入与更新操作
  16. 微信小程序入门(二)
  17. fail2ban[防止linux服务器被暴力破解]
  18. CentOS7 Windows双系统 修复引导
  19. wget整站抓取、网站抓取功能;下载整个网站;下载网站到本地
  20. 基于uFUN开发板的心率计(一)DMA方式获取传感器数据

热门文章

  1. 图片上传转base64
  2. Windows Phone 页面切换动画
  3. 监控linux流量shell版
  4. Zend_Framework_1 框架是如何被启动的?
  5. 【转载】排查Linux机器是否已经被入侵
  6. 170508、忘记jenkins密码或者修改jenkins密码
  7. shiro框架的学习
  8. poj3449 Geometric Shapes【计算几何】
  9. MyBatis 的真正强大在于它的映射语句 如果有一个独立且完美的数据库映射模式,所有应用程序都可以使用它
  10. gnome,xfce,unity,vncserver chinese,jvm locale language