Блог пользователя Sacha

Автор Sacha, 22 месяца назад, перевод, По-русски

Есть идеи?

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится -37 Проголосовать: не нравится

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.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +78 Проголосовать: не нравится

Isn't this just Suffix Array?

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

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

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This cf task can be reduced to the problem above.