11019 Matrix Matcher
Given an N × M matrix, your task is to find the number of occurences of an X × Y pattern.
Input
The first line contains a single integer t (t ≤ 15), the number of test cases.For each case, the first line contains two integers N and M (N, M ≤ 1000). The next N linescontain M characters each.The next line contains two integers X and Y (X, Y ≤ 100). The next X lines contain Y characterseach.
Output
For each case, output a single integer in its own line, the number of occurrences.
Sample Input
21 1x1 1y3 3abcbcdcde2 2bccd
Sample Output
02
解题思路:大矩阵(m*n)小矩阵(x*y)将大矩阵每一行hash,大矩阵每一列储存包括他本身在内 y 列的信息,小矩阵也按照这种方式压缩成 x*1 的矩阵,然后将大矩阵存储的那些数值按照一列一列的储存进一个数组中,每一列之间用一个不可能出现的数字分割,然后在大矩阵的hash数组中跑小矩阵hash数组的kmp算法。
AC代码:
/*
@Author: wchhlbt
@Date: 2017/8/7
*/
//#include <bits/stdc++.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>
#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 1003
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
#define _ << " " <<
using namespace std;
typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
inline int read(){ int num; scanf("%d",&num); return num;}
int n,m;
int ans;
/*
* 把字符串变成X进制数,可以完成$ O(1) $比较 ,
* 调用 Init() 初始化幂 ,
* 调用 Init(u64* Hash, int len) 初始化 Hash 数组 ,
* Get(u64* Hash, int p, int L) 表示获得以 p 开头长度为 L 的字符串 Hash 。
* Base 需要选用质数
*/
typedef unsigned long long u64;
const u64 Base = 31;
const int N = 1003;
char s[N][N];
u64 Hash[N];
u64 Pow[N];
void Init() {
Pow[0] = 1;
for(int i = 1; i < N; i++) Pow[i] = Pow[i - 1] * Base;
}
void Init(int id, int len) {
Hash[len] = 0;
for(int i = len - 1; i >= 0; i--)
Hash[i] = (u64)Hash[i + 1] * Base + (s[id][i] - 'a' + 1);
}
u64 Get(int p, int L) {
return Hash[p] - Hash[p + L] * Pow[L];
}
u64 dic[maxn][maxn];
//KMP
u64 p[10003];
u64 a[1000007];
int f[maxn];//失配函数
void getFail(int plen)
{
f[0] = 0; f[1] = 0;
for(int i = 1; i<plen; i++){
int j = f[i];
while(j && p[i]!=p[j]) j = f[j];
f[i+1] = (p[i]==p[j]) ? j+1 : 0;
}
}
int find(int slen, int plen)//s->待匹配文本串 p->模板串
{
getFail(plen);
int j = 0;
int cnt = 0;
for(int i = 0; i<slen; i++){
while(j && a[i]!=p[j]) j = f[j];
if(a[i]==p[j])
j++;
if(j==plen){
j = f[j];//不考虑重叠时,此处置0
//return i + 1 -plen; 返回第一个匹配的位置
cnt++;
}
}
return cnt;
}
int main()
{
//freopen("test.txt","w",stdout);
int t = read();
Init();
while(t--){
int n = read(); int m = read();
int cnt = 0;
for(int i = 1; i<=n; i++){
scanf("%s",s[i]);
}
int x = read(); int y = read();
int slen;
for(int i = 1; i<=n; i++){
slen = strlen(s[i]);
Init(i,slen);
for(int j = 0; j+y<=slen; j++)
dic[i][j] = Get(j,y);
}
for(int i = 1; i<=x; i++){
scanf("%s",s[i]);
int len = strlen(s[i]);
Init(i,len);
p[cnt++] = Hash[0];
}
int plen = cnt;
cnt = 0;
for(int j = 0; j+y<=slen; j++){
for(int i = 1; i<=n; i++){
a[cnt++] = dic[i][j];
}
a[cnt++] = -1;
}
int alen = cnt;
printf("%d\n", find(alen,plen));
}
return 0;
}
/*
unsigned int 0~4294967295
int 2147483648~2147483647
unsigned long 0~4294967295
long 2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:18446744073709551615
__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615
*/