1. Title Description: Click to open the link
2. Problem-solving ideas: The subject can be solved with AC automata, but found that this method of time efficiency is relatively low, personal recommendation to use two-dimensional hash to solve the problem. After testing on OJ, the method of AC automata requires more than 1s, while the two-dimensional hash requires less than 100ms! So here's how to use a two-dimensional hash to solve the problem.
First of all, any hash technique needs to be given a function, so that the less collisions the different strings have to calculate the hash value, the better. For the character matrix, we generally use two-dimensional hash to deal with. Although it is a two-dimensional, but the principle and one-dimensional similar, that is, first of each line of the first J characters for one-dimensional hash, the first I row to deal with, I row 1 column hash value, next to this I row 1 column hash value again hash, the resulting hash value is (I,J) at the hash value. This is written as a recursive style:
In the formula, each line of hash time, seed=q; each column is hashed, seed=p, here is a simple example to illustrate this method.
Suppose the current character matrix is as follows:
For the matrix to calculate the hash value of each lattice according to the above formula, we can get the following hash matrix:
It is not difficult to find out, two-dimensional hash is indeed 2 one-dimensional hash of the compound process. Moreover, if the p,q selection is appropriate, you can guarantee (I,J) The hash value can be considered as sub-matrix (0,0) ~ (i,j) hash value, and as long as the sub-matrix is not the same, then the corresponding hash value must be different. In the following, we use the hash value in the lower right corner of the matrix to represent the hash value of the entire matrix.
So how do you calculate the hash value of this sub-matrix (i,j) ~ (i+x-1,j+y-1) from this matrix? or using the initial formula, you can get the following formula only by iterating over and over again:
Thus, with the above analysis, the problem is not difficult to solve, first calculate the original character matrix hash matrix, while calculating the input p matrix hash value H, by enumerating X*y sub-matrix, and calculate the sub-matrix hash value is equal to H, if it is, then ans++. Finally, you get the answer.
The time complexity of the subject is O ((n-x) * (m-y)).
3. Code:
#include <iostream> #include <algorithm> #include <cassert> #include <string> #include < sstream> #include <set> #include <bitset> #include <vector> #include <stack> #include <map > #include <queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring > #include <cmath> #include <ctime> #include <cctype> #include <functional> #pragma comment ( linker, "/stack:1024000000,1024000000") using namespace std; #define ME (s) memset (s,0,sizeof (s)) #define REP (I,n) for ( int i=0;i< (n); i++) typedef long LONG ll;typedef unsigned int uint;typedef unsigned long long ull;//typedef pair <int, int> p;const int n=1101;const int p=131;const int q=1331;ll hash[n][n];ll hv[110][110];ll Powp[n],powq[n];char Str[N]; void init ()//Initialize the result of P^i,q^i {powp[0]=powq[0]=1; for (int i=1;i<n;i++) {powp[i]=powp[i-1]*p; Powq[i]=powq[i-1]*q; }}int Main () {int T; scanf ("%d", &t); Init (); WhIle (t--) {int n,m; scanf ("%d%d", &n,&m); Me (Hash); me (HV); for (int i=1;i<=n;i++) {scanf ("%s", str+1); for (int j=1;j<=m;j++) hash[i][j]=hash[i-1][j]*p+hash[i][j-1]*q-hash[i-1][j-1]*p*q+str[j];//compute Hash Matrix } int x, y; scanf ("%d%d", &x,&y); for (int i=1;i<=x;i++) {scanf ("%s", str+1); for (int j=1;j<=y;j++) hv[i][j]=hv[i-1][j]*p+hv[i][j-1]*q-hv[i-1][j-1]*p*q+str[j];//calculates the hash value of the P-matrix} ll H; int ans=0; for (int i=1;i<=n-x+1;i++) for (int j=1;j<=m-y+1;j++)//enum X*y sub-matrix {h=hash[i+x-1][j+y-1]-h Ash[i-1][j+y-1]*powp[x]-hash[i+x-1][j-1]*powq[y]+hash[i-1][j-1]*powp[x]*powq[y]; if (H==hv[x][y]) ans++; Equal, then ans++} printf ("%d\n", ans); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Example 3.16 Matrix Match UVa11019