http://codeforces.net/problemset/problem/182/D
this problem is tagged with hashing
but i couldnt find any tutorial for it. can you please give me an idea for solving it by use of hashing? thanks.
# | 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 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
http://codeforces.net/problemset/problem/182/D
this problem is tagged with hashing
but i couldnt find any tutorial for it. can you please give me an idea for solving it by use of hashing? thanks.
Name |
---|
Solution: find all divisors of each string, and find common of them.
How to find all divisors? We know that divisor D is prefix of string S and |S| = 0 (mod |D|). So, amount of divisors is at maximum sqrt(|S|). Let's check all prefixes of string which length is <= sqrt(|S|) and check if we write this prefix |S| / |D| times it will be same as S. If it is, then prefix D is the divisor of S. How to check it? Check if substring [0, |T|) is same as [|T|, 2 * |T|) and [2 * |T|, 3 * |T|) and so on.. by using hash. Total time is |S| * sqrt(|S|). After, do the same to second string. You will have a two arrays of divisors of each string (the array of hashes). Then, you calculate how many divisors are common.
That's all.
ur wrong.
aaaa
has divisors (a
,aa
, andaaaa
), but .yes you are correct. can you suggest how to solve this?
Yes, he's wrong. there r no more 2 * sqrt(|S|) divisors. And, BTW, this problem can solved without using any string algorithm. See my solution. Sry for bad eng.
Omg, authors solution is really strange. We can find all divisors of the string in O(n) using Z-algorithm or KMP. Hashing can be used to compare divisors, but it could be simply done in naive way in O(n) totally for all divisors.
what do you mean by naive way? comparing all divisors char by char should be O(n^2), right?
Nope, it's not. 6729772
There are not that many divisors for a given N. So in fact the complexity is something around O(n4 / 3)
This discussion in Russian brings the estimate for cubic root: http://codeforces.net/blog/entry/651. You can google translate it or just read the links from it.
Pretty cool solution :D
All divisors are prefixes of the strings. So, we can check all common prefixes if they are divisors in both strings.
hey i am solving it with hashing but i am getting WA. same code gives correct answer on IDEONE http://ideone.com/1cv777 http://codeforces.net/contest/182/submission/6734760
someone please help me in debugging. i using lots of printfs but still i am unable to figure out the source of error. as i am yet to get AC on a hashing problem i dont feel very confident about it.
I have changed both
into
and it passed first three tests :D