AreluF's blog

By AreluF, history, 4 years ago, In English

Can somebody explain me how to solve Matrix Matcher using Hashing. link : https://onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1960

Thanks in advance.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by AreluF (previous revision, new revision, compare).

»
4 years ago, # |
Rev. 3   Vote: I like it +3 Vote: I do not like it

Hash of symbols on $$$i$$$ line at $$$j$$$ position equal: $$$h(a_{ij})=a_{ij}\cdot P^i \cdot Q^j \mod M$$$, where $$$P, Q, M$$$ mutually prime numbers (for example $$$P=10007, Q = 10009, M=1000000007$$$).

Hash of matrix equals to the sum of all elements: $$$H(a)=\sum_{i=0}^{n-1} \sum_{j=0}^{m-1}h(a_{ij}) \mod M$$$, where $$$n$$$ — count rows, $$$m$$$ — count columns.

Calculate the prefix sum $$$p$$$ for hash of matrix $$$a$$$: $$$p_{ij}= \sum_{i_1=0}^{i-1} \sum_{j_1=0}^{j-1}h(a_{i_1j_1}) \mod M$$$. Then hash of submatrix starting in element $$$a_{i_1j_1}$$$and ending of element $$$a_{i_2j_2}$$$ equal: $$$\frac{p_{i_2+1j_2+1} - p_{i_1j_2+1} - p_{i_2+1j_1} + p_{i_1j_1}}{P^{i_{1}}\cdot Q^{j_{1}}} \mod M$$$.

You can iterate over all starting elements and compare hash submatrix and matrix $$$b$$$. Moreover, you may not implement modulo division if you replace it with multiplication.