zoukankan      html  css  js  c++  java
  • 【BZOJ 2351】 Matrix

    【题目链接】

                https://www.lydsy.com/JudgeOnline/problem.php?id=2351

    【算法】

                哈希

    【代码】

                 

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 1010
    typedef unsigned long long ull;
    const int base1 = 131;
    const int base2 = 211;
    const int P = 1e5 + 7;
    
    int i,j,m,n,a,b,q;
    ull sum[MAXN][MAXN];
    ull power1[MAXN],power2[MAXN];
    ull t;
    vector< ull > e[P];
    char c;
    char s[MAXN][MAXN];
    
    inline void insert(ull x)
    {
            int h = (int)(x % P);
            e[h].push_back(x);        
    }
    inline bool query(ull x)
    {
            int i;
            int h = (int)(x % P);
            for (i = 0; i < (int)e[h].size(); i++)
            {
                    if (e[h][i] == x)
                            return true;
            }
            return false;
    }
     
    int main() 
    {
            
            scanf("%d%d%d%d",&m,&n,&a,&b);
            for (i = 1; i <= m; i++) scanf("%s",s[i]+1);
            for (i = 1; i <= m; i++)
            {
                    for (j = 1; j <= n; j++)
                    {
                            sum[i][j] = s[i][j] - '0';
                    }
            }
            for (i = 1; i <= m; i++)
            {
                    for (j = 1; j <= n; j++)
                    {
                            sum[i][j] += sum[i-1][j] * base1;
                    }
            }
            for (i = 1; i <= m; i++)
            {
                    for (j = 1; j <= n; j++)
                    {
                            sum[i][j] += sum[i][j-1] * base2;
                    }
            }
            power1[0] = power2[0] = 1;
            for (i = 1; i < m; i++)
            {
                    power1[i] = power1[i-1] * base1;
                    power2[i] = power2[i-1] * base2;
            }
            for (i = a; i <= m; i++)
            {
                    for (j = b; j <= n; j++)
                    {
                            t = sum[i][j] - sum[i-a][j] * power1[a] - sum[i][j-b] * power2[b] + sum[i-a][j-b] * power1[a] * power2[b];
                            insert(t); 
                    }
            }
            scanf("%d",&q);
            while (q--)
            {
                    for (i = 1; i <= a; i++) scanf("%s",s[i]+1);
                    for (i = 1; i <= a; i++)
                    {
                            for (j = 1; j <= b; j++)
                            {
                                    sum[i][j] = s[i][j] - '0';
                            }
                    }
                    for (i = 1; i <= a; i++)
                    {
                            for (j = 1; j <= b; j++)
                            {
                                    sum[i][j] += sum[i-1][j] * base1;
                            }
                    }
                    for (i = 1; i <= a; i++)
                    {
                            for (j = 1; j <= b; j++)
                            {
                                    sum[i][j] += sum[i][j-1] * base2;
                            }
                    }
                    if (query(sum[a][b])) printf("1
    ");
                    else printf("0
    ");
            }
            
            return 0;
        
    }
  • 相关阅读:
    cuda(2)---方阵乘法
    cuda(1)-imageBlur
    python(6) 字符串操作
    CUDA 编程之Release模式和Debug模式
    20200909 day4 刷题记录
    20200908 day3 刷题记录
    20200906 day1 模拟(一)
    刷题Day 4-6 树形dp三题
    4.28 刷题Day 3 树形dp一题
    DTQ2019-D1T2 括号树 题解
  • 原文地址:https://www.cnblogs.com/evenbao/p/9282310.html
Copyright © 2011-2022 走看看