Bzoj 2462 Beijing 2011 Matrix template Two-dimensional hash

Source: Internet
Author: User

The main topic: Give a m*n 01 matrix, the following Q queries, the query matrix exists in the size of the K*l sub-matrix.


Idea: two-dimensional hash. We first hash the big matrix and then insert all the possible k*l sub-matrices into the hash table, and then just look at the hash tables for each query hash to see if they exist.

It is worth mentioning that the problem only requires the output of 10 1 can be AC.


CODE:

#include <cstdio> #include <bitset> #include <cstring> #include <iostream> #include < Algorithm> #define MAX 1100using namespace std;const unsigned int BASE1 = 10016957;const unsigned int BASE2 = 10016957;c onst int MO = 99999997; int m,n,ask_m,ask_n,asks;unsigned int hash[max][max],_hash[max][max];unsigned int Pow1[max],pow2[max]; BOOL set[100000000];            inline unsigned int gethash () {for (int i = 1, i <= ask_m; ++i) for (int j = 1; j <= Ask_n; ++j)    _HASH[I][J] + = _hash[i-1][j] * BASE1; for (int i = 1, i <= ask_m; ++i) for (int j = 1; j <= ask_n; ++j) _hash[i][j] + = _hash[i][j-1] * B    ASE2; return _hash[ask_m][ask_n];}    int main () {cin >> m >> n >> ask_m >> ask_n;    for (int i = 1; I <= m; ++i) for (int j = 1; j <= N; ++j) scanf ("%1d", &hash[i][j]);    Pow1[0] = pow2[0] = 1; for (int i = 1; i <=; ++i) pow1[i] = pow1[i-1] * Base1,pow2[i] = Pow2[I-1] * BASE2;    for (int i = 1, i <= m; ++i) for (int j = 1; j <= N; ++j) hash[i][j] + = hash[i-1][j] * BASE1;    for (int i = 1, i <= m; ++i) for (int j = 1; j <= N; ++j) hash[i][j] + = hash[i][j-1] * BASE2;            for (int i = ask_m, I <= m; ++i) for (int j = ask_n; J <= N; ++j) {unsigned int h = hash[i][j];            H-= hash[i-ask_m][j] * Pow1[ask_m];            H-= hash[i][j-ask_n] * Pow2[ask_n];            H + = hash[i-ask_m][j-ask_n] * pow1[ask_m] * Pow2[ask_n];        Set[h% MO] = true; } for (Cin >> asks; asks--;) {for (int i = 1, i <= ask_m; ++i) for (int j = 1; j <= ask_n; ++j) scanf ("%1d",&        _HASH[I][J]); Puts (Set[gethash ()% MO]?    "1": "0"); } return 0;}


Bzoj 2462 Beijing 2011 Matrix template Two-dimensional hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.