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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.