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
По твоему коду: Во-первых, убери s[i] - 'a' + 1 — в задаче могут быть символы начиная с 33 кода. Во-вторых сделай p разными, и больше чем 53 — хотя бы больше чем размер алфавита
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)
Auto comment: topic has been translated by Kane (original revision, translated revision, compare)
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
Построим строку А, такую что А = S + S. Теперь, мы должны найти первое вхождение строки Т в А. Это можно сделать очень легко с KMP.
Спасибо. KMP это очень круто, не спорю. Но хотелось бы сдать именно двойными хешами.
http://ideone.com/fAZQpx вот, если очень нужно хешами — код на java, но суть понятна
PS: там и одинарный заходит — http://ideone.com/zBAMZh
По твоему коду: Во-первых, убери
s[i] - 'a' + 1
— в задаче могут быть символы начиная с 33 кода. Во-вторых сделай p разными, и больше чем 53 — хотя бы больше чем размер алфавита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)
Везде пишу двойные хэши от Burunduk1 и всегда работает) Ссылка на его пост про хэши: http://codeforces.com/blog/entry/17507