POJ 3690(Constellations-矩阵hash)

在一个n*m的矩阵里查t个p*q的子矩阵是否存在。
(1 ≤ N, M ≤ 1000, 1 ≤ T ≤ 100, 1 ≤ P, Q ≤ 50)

矩阵hash,写精简一点(我的代码不精简),
要在map里放N*M个矩阵为TLE,
所以在map里放t个子矩阵

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<string>
#include<cctype>
#include<map>
#include<ctime>
#include<iomanip> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) 
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define Pr(kcase,ans) printf("Case %d: %lld\n",kcase,ans);
typedef long long ll;
typedef unsigned long long ull;

ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
ull base2=103;
const ll base=2;
ll p2[55]={0};
int n,m,p,q,t,ans;
#define MAXN (1010)
map<ull,int> hhh;
struct Mart{
    ll a[MAXN][MAXN];
    ull h[MAXN][MAXN];
    int n,m;
    void init(int n,int m ) {
        this->n=n;  this->m=m;
        string s;
        Rep(i,n) {
            cin>>s;
            Rep(j,m) if  (s[j]=='*') a[i][j]=1; else a[i][j]=0; 
        }
        hash_j();
    }
    void hash_j(){
        Rep(j,m)
        {
            h[0][j]=a[0][j];
            For(i,n-1) {
                h[i][j]=h[i-1][j]*base+a[i][j];
            } 
        }
    }
    ull hash2(int l,int r,int j) {
        if (l==-1) return h[r][j];
        return h[r][j]-h[l][j]*p2[r-l]; 
    }
    ll hh[MAXN];

    void hash3(int l,int r){
        hh[0]=hash2(l,r,0);
        For(i,m-1) hh[i]=hh[i-1]*base2+hash2(l,r,i);
    }
    void hash4() {
        map<ull,int>::iterator it = hhh.find(hh[q-1]);
        if (it!=hhh.end()) {
            ans+=it->se;
            hhh.erase(it);
        }
        ull base3=1;
        For(i,q) base3*=base2;
        Fork(i,q,m-1) {
            it = hhh.find((ull)(hh[i]-hh[i-q]* base3));
            if (it!=hhh.end()) {
                ans+=it->se;
                hhh.erase(it);
            }
        }
    }

}A,P;
int main()
{
// freopen("A.in","r",stdin);
// freopen("A.out","w",stdout);
    int kcase=1;
    p2[0]=1;
    For(i,51) p2[i]=p2[i-1]*base;
    while(1) {
        hhh.clear();
        ans=0;
        cin>>n>>m>>t>>p>>q;
        if (n==0) break; 
        A.init(n,m);
        while(t--) {
            P.init(p,q);
            P.hash3(-1,p-1);
            if (!hhh[P.hh[q-1]]) hhh[P.hh[q-1]]=1; else hhh[P.hh[q-1]]++;
        }
        for(int i=0;i-1+p<n;i++) {
            A.hash3(i-1,i-1+p);
            A.hash4();
        }
        Pr(kcase++,ans)
    }


    return 0;
}

你可能感兴趣的:(POJ 3690(Constellations-矩阵hash))