Can someone provide me some resource or code for fast hashing that can be used in topcoder SRM's/Codeforces rounds. OR I have to use set/map for hashing purposes.
Suggestion Welcome For: In what kind of problems hashing to be used.
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Can someone provide me some resource or code for fast hashing that can be used in topcoder SRM's/Codeforces rounds. OR I have to use set/map for hashing purposes.
Suggestion Welcome For: In what kind of problems hashing to be used.
Name |
---|
Sometimes you face a string processing problem for which you have the correct idea, but implementing it might not be so easy. In some of these cases, you can find a much easier to implement algorithm using hashing. These algorithms don't always give correct answer, but their probability of giving wrong answer is very low.
The simplest example is string matching, where you have to find a string inside another string. Well, you can use the KMP algorithm, which is fast enough, and easy to implement if you have enough practice. There's an alternative using hashing: Rabin-Karp algorithm. You probably should know it so you can invent more powerful algorithms for more difficult problems.
What you're looking for with strings is most likely polynomial hashing. If you have a string (in which we could represent the characters as numbers a[i] from the range [1,x]), its prefix of i characters has the hash
A[i]=sum(j=1..i)(a[i]*p^i)%M
where p and M are suitably selected primes (I use p close to max. string length, M as large as allowed — for strings of letters with length up to 1000000 p is the first prime below 1000000 and M is 1000000009.)
Another nice characteristic of this hash is that it works like prefix sums with precomputation of power of p.
You can read this site Here. It's well written .