UVALive 6893 The Big Painting hash
The Big Painting
题目连接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/J
Description
Samuel W. E. R. Craft is an artist with a growing reputation.
Unfortunately, the paintings he sells do not provide
him enough money for his daily expenses plus the new supplies
he needs. He had a brilliant idea yesterday when he
ran out of blank canvas: ”Why don’t I create a gigantic
new painting, made of all the unsellable paintings I have,
stitched together?”. After a full day of work, his masterpiece
was complete.
That’s when he received an unexpected phone call: a
client saw a photograph of one of his paintings and is willing
to buy it now! He had forgotten to tell the art gallery to
remove his old works from the catalog! He would usually
welcome a call like this, but how is he going to find his old
work in the huge figure in front of him?
Given a black-and-white representation of his original
painting and a black-and-white representation of his masterpiece, can you help S.W.E.R.C. identify in
how many locations his painting might be?
Input
The input file contains several test cases, each of them as described below.
The first line consists of 4 space-separated integers: hp wp hm wm, the height and width of the
painting he needs to find, and the height and width of his masterpiece, respectively.
The next hp lines have wp lower-case characters representing his painting. After that, the next hm
lines have wm lower-case characters representing his masterpiece. Each character will be either ‘x’ or
‘o’.
Constraints:
1 ≤ hp, wp ≤ 2 000
1 ≤ hm, wm ≤ 2 000
hp ≤ hm
wp ≤ wm
Output
The painting could be in four locations as shown in the following picture. Two of the locations overlap
Sample Input
4 4 10 10
oxxo
xoox
xoox
oxxo
xxxxxxoxxo
oxxoooxoox
xooxxxxoox
xooxxxoxxo
oxxoxxxxxx
ooooxxxxxx
xxxoxxoxxo
oooxooxoox
oooxooxoox
xxxoxxoxxo
Sample Output
4
Hint
题意
给你两个只含有ox的矩阵,问你第二个矩阵内有多少个第一个矩阵
题解:
标准的二维矩阵匹配,但是这道题数据范围太大了,ac自动机T成傻逼,不要问我为什么
所以就写hash吧。。。。
代码
#include <bits/stdc++.h>
using namespace std;
const long long pr=1e9+7;
const int N=2005;
const long long p=233LL;
char s[N];
long long hl[N*N],h[N][N],xh[N][N],a[N][N],b[N][N],tt;
int main()
{
int hp,wp,hm,wm;
while(scanf("%d%d%d%d",&hp,&wp,&hm,&wm)!=EOF)
{
memset(h,0,sizeof(h));
memset(xh,0,sizeof(xh));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
hl[0]=1LL;
for(int i=1;i<=wp*hp;i++)
{
hl[i]=(hl[i-1]*p)%pr;
}
tt=0;
for(int i=1;i<=hp;i++)
{
scanf("%s",s+1);
for(int j=1;j<=wp;j++)
{
if(s[j]=='o') a[i][j]=0;else a[i][j]=1;
tt=(tt*p+a[i][j])%pr;
}
}
for(int i=1;i<=hm;i++)
{
scanf("%s",s+1);
xh[i][0]=0;
for(int j=1;j<=wm;j++)
{
if(s[j]=='o') b[i][j]=0;else b[i][j]=1;
xh[i][j]=(xh[i][j-1]*p+b[i][j])%pr;
}
}
int cnt=0;
for(int j=1;j+wp-1<=wm;j++)
{
h[1][j]=0;
for(int k=1;k<=hp;k++)
h[1][j]=(h[1][j]*hl[wp]+(xh[k][j+wp-1]-xh[k][j-1]*hl[wp]%pr)+pr)%pr;
if(h[1][j]==tt) cnt++;
for(int i=2;i+hp-1<=hm;i++)
{
h[i][j]=((h[i-1][j]-(xh[i-1][j+wp-1]-xh[i-1][j-1]*hl[wp]%pr)*hl[wp*hp-wp]%pr)*hl[wp]%pr+
(xh[i+hp-1][j+wp-1]-xh[i+hp-1][j-1]*hl[wp]%pr)+pr+pr)%pr;
if(h[i][j]==tt) cnt++;
}
}
cout<<cnt<<endl;
}
return 0;
}