ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1423. String Tale

Hint
Posted by ASK 3 Mar 2010 21:29
Similar to <http://en.wikipedia.org/wiki/Rabin-Karp_string_search_algorithm>

Think of a string as coefficients of a polynomial H (calculated modulo some, say, 31-bit prime P, e.g., 2000000011). Take a random X and calculate H(X). Take the second string and calculate its code. To calculate the value for a shifted string simply add to it A*(1-X^n), where A is the i-th character (this is a simplification of the needed steps: subtract A*X^{n-1}, multiply by X, and add A). If value for the shifted string is the same as for the first, output i (if P is large you don't even need to check).
Re: Hint
Posted by svr 3 Mar 2010 22:45
Kruto!
Re: Hint
Posted by Anton 29 Nov 2011 07:05
Very nice idea. But I used some edition in my solution: I don't use any prime number, I just use unsigned long long type without any modulo operation. So the type overflow does its work well enough and It's no nessesary to check strings for eq.