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.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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.
Name |
---|
Auto comment: topic has been updated by AreluF (previous revision, new revision, compare).
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.