题目链接:Constellations
题解:对于每个模式子矩阵对应的hash值放入multiset,然后对象矩阵每个位置的hash值求出在删除multiset中对应的hash值
//#include<bits/stdc++.h>
#include<set>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define pb push_back
//#define ll long long
#define PI 3.14159265
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define eps 1e-7
typedef unsigned long long ull;
int N,M,T,p,q;
const ull b1=9973;
const ull b2=100000007;
const int mod=1e9+7;
const int maxn=1e3+5;
using namespace std;
char fi[maxn][maxn];
char pat[maxn][maxn];
ull hash1[maxn][maxn],tmp[maxn][maxn];
void compute_hash(char a[maxn][maxn],int n,int m)
{ull t1=1;for(int i=0;i<q;i++)t1*=b1;for(int i=0;i<n;i++){ull e=0;for(int j=0;j<q;j++)e=e*b1+a[i][j];for(int j=0;j+q<=m;j++){tmp[i][j]=e;if(j+q<m)e=e*b1-a[i][j]*t1+a[i][j+q];}}ull t2=1;for(int i=0;i<p;i++)t2=t2*b2;for(int j=0;j+q<=m;j++){ull e=0;for(int i=0;i<p;i++)e=e*b2+tmp[i][j];for(int i=0;i+p<=n;i++){hash1[i][j]=e;if(i+p<n)e=e*b2-tmp[i][j]*t2+tmp[i+p][j];}}
}
int main()
{std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int TT=1;while(cin>>N>>M>>T>>p>>q){if(N==0&&M==0&&T==0&&p==0&&q==0)break;for(int i=0;i<N;i++){for(int j=0;j<M;j++){cin>>fi[i][j];}}int tt=T;multiset<ull>unseen;unseen.clear();while(tt--){for(int i=0;i<p;i++){for(int j=0;j<q;j++){cin>>pat[i][j];}}compute_hash(pat,p,q);unseen.insert(hash1[0][0]);}compute_hash(fi,N,M);for(int i=0;i+p<=N;i++){for(int j=0;j+q<=M;j++){unseen.erase(hash1[i][j]);}}cout<<"Case "<<TT++<<": "<<T-unseen.size()<<endl;}return 0;
}