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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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 .