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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
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