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

Автор YouKn0wWho, история, 8 лет назад, По-английски

I am stuck on this following problem:

Statement:

Given a string of size n and q queries of type (l,r) you need to find how many palindromes are there in the substring of range [l...r].

Constraints:

1<=n,q<=100000

1<=l<=r<=n

Thanks for reading the problem. It will be really helpful if you can give me a solution.

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

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Mistake in approach.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Sqrt-decomposition solution is costly but may pass, depending on TL.

Whenever you add/remove some index, you have to add/remove palindromic substrings ending at that index, which you can find using binary search (hash the string and it's reverse).

Total complexity will be

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

It is easier to explain by considering only palindromes centered at indicies (so, odd length), the idea is the same anyway. For each index i, ri will be the longest radius of a palindrome centered there (in other words, the amount of palindromes centered at index i). Directly from manacher, this takes to calculate.

For a query [l, r], we first compute . Now we want to calculate

Again, thanks to symmetry, I'll only explain how to compute the left term.

.

The sum over i can be found in constant time. As for the other term, if we create some array ri' = ri - i during the preprocessing, then the queries are asking for some over range of min(C, ri') where C is constant. You can solve this in per query using wavelet tree, and I guess it's also solvable with a persistent segtree.

  • »
    »
    8 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +5 Проголосовать: не нравится

    Thanks for your reply. This is an interesting approach.

    Well, I am interested to know about this idea- min(i-l+1,r[i])=i+min(1-l,r[i]-i).

    I have brute forced for different i,l and r[i] and found that this is true. But curious mind wants to know the proof of this. If you can come up with a proof feel free to share this with me.

    Thanks again.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Palindrome tree can tall you, how many palindrome add s[i]. You can create partial sum array and say on query O(1).
Sorry for my English.

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

Please provide problem link.I want to solve it.

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

My approach takes O(n ^ 2) precalculation to get O(1) queries.

Can I improve furthur more with dynamic programming ?

My code