Kane's blog

By Kane, history, 8 years ago, translation, In English

I solve a problem on the Timus Online Judge.

Submit with hashing gets WA31(because of antihash). But submit with double hashing gets WA8...

Help me, please, with double hashing and solve this problem.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been translated by Kane (original revision, translated revision, compare)

»
8 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

I first tried solving it with double hashing with primes 31 and 37. I got WA on Test 9. I changed the modulo, again WA on Test 9, so I realized it was because some hash was becoming zero along the way, and that's certainly not good when hashing...

My workaround was to make a list of primes and choose two randomly until all hashes are positive. I got AC with that. Here's the code -> C++ Double Hashing

»
8 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

I understand that you're asking specifically for the hashing solution, so you probably already know, but this problem can be solved simply by adding a copy of S to itself and then running KMP.

Here's the code for this solution: KMP Code (the hashing solution is above)