Any ideas?
# | 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 | 170 |
2 | Um_nik | 162 |
3 | atcoder_official | 160 |
3 | maomao90 | 160 |
5 | djm03178 | 158 |
5 | -is-this-fft- | 158 |
7 | adamant | 154 |
7 | Dominater069 | 154 |
9 | awoo | 152 |
9 | luogu_official | 152 |
Any ideas?
Name |
---|
For a string of length $$$O(\sqrt{n\log{n}})$$$ you can bruteforce all endpoints of substrings, calculate their hashes and count them in unordered map. The answer is the sum of $$$\frac{v (v - 1)}{2}$$$ over all pairs $$$(k,v)$$$ in the map. Total complexity is $$$O(\sqrt{n\log{n}}^2) = O(n\log{n})$$$ time and $$$O(distinct\ substrings)$$$ memory.
Jokes aside, this solution isn't even $$$\mathcal{O}(n^2)$$$. It's $$$\mathcal{O}(n^3)$$$. Each hash calculation takes linear time. Unless you write a custom hash calculation method which can be updated incrementally as you add chars.
Read about rolling hashes, bro, my analysis if fully correct.
Isn't this just Suffix Array?
Build a suffix array, the answer for some suffix starting from index i will be the sum of lcp(i,j) for all j such that j<i. You can calculate the answer using a lazy segtree which maintains the counts of lcp(i,j) for j<i, when you transition from i to i+1, just update the answer according to the value of lcp(i,i+1). If the pairs are ordered just multiple the final ans by 2.
we can do it with suffix array and lcp
imagine the lcp array form a histogram where the height is the length and the width is the frequency then if we used monotonic stack to get the prev greater, next greater we will be able to know for each substr how many times it exists
then for each length from 1 to lcp[i] it should has duplicates equal to the width in the histogram