UVA 11019 Matrix Matcher

hash大法好,hash出奇迹。

厉害学弟写厉害的AC自动机,不会,只能拼RP写HASH

。。。

#include<stdio.h>
#include<string.h>
#include<set>
#include<map>
#include<algorithm>
using namespace std;

#define rep(i,s,t) for(int i=s;i<t;i++)
typedef unsigned long long ull;

const ull B1=10000007;
const ull B2=100000007;

int t,n,m,x,y,ans;
char s[1001][1001],a[101][101];
ull p;
ull tmp[1001][1001],ttmp[1001][1001];

inline ull Hash(){
	ull c,d=0;
	rep(i,0,x){
		c=0;
		rep(j,0,y){
			c=c*B1+a[i][j];
		}
		d=d*B2+c;
	}
	return d;
}

inline void getAns(){
	ull t,a;
	t=1;rep(i,0,y) t*=B1; 
	rep(i,0,n){
		a=0;
		rep(j,0,y) a=a*B1+s[i][j];
		tmp[i][y-1]=a;
		rep(j,y,m){
			tmp[i][j]=tmp[i][j-1]*B1-s[i][j-y]*t+s[i][j];
		}
	}
	t=1;rep(i,0,x) t*=B2;
	rep(i,y-1,m){
		a=0;
		rep(j,0,x) a=a*B2+tmp[j][i];
		ttmp[x-1][i]=a;
		if(a==p) ans++;
		rep(j,x,n){
			ttmp[j][i]=ttmp[j-1][i]*B2-tmp[j-x][i]*t+tmp[j][i];
			if(ttmp[j][i]==p) ans++;
		}
	}
}
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		rep(i,0,n) scanf("%s",s[i]);
		scanf("%d%d",&x,&y);
		rep(i,0,x) scanf("%s",a[i]);
		p=Hash();ans=0;
		getAns();
		printf("%d\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值