Блог пользователя AreluF

Автор AreluF, история, 4 года назад, По-английски

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.

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
4 года назад, # |
Rev. 3   Проголосовать: нравится +3 Проголосовать: не нравится

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.