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

Автор Valour, 7 лет назад, По-английски

Can someone please help with this problem.

Given a string (s) and some queries where each query is of the form [l, r] we need to find the number of palindromic substrings, where the length of each substring lies in between [l, r].

Constraints
1 <= |S| <= 100000
1 <= |q| <= 100000
1 <= l < r <= |S|

Sample Case:
s = "zz"
q = 1
[l, r] = [1, 2]

For this, the answer must be 3. 'z', 'z', 'zz' are the three substrings which are palindrome and their length in between [1, 2].

The substrings to be considered may not be distinct, i.e if s = "zzz" then "zz" is to be considered twice.

Thanks in Advance!

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

»
7 лет назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

I think it can be solved by algorithm "mannaker"!

But also by DP here

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +13 Проголосовать: не нравится

consider each index of your string as a center of palindrome and do a binary search on maximum length string possible using this point as a center. all lengths smaller than that are also possible.You can compute prefix and suffix hash and just see if they are same or not for the length calculation part which can be done in O(1) with O(n) precomputation so your overall ur complexity will be nlogn and for updating lengths you can use bit.and when ur done with this precomputation its just a range query in ur bit

»
7 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

I think this can be solved using Manacher's algorithm and prefix sums.

  • »
    »
    7 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Hello!

    Thanks for your response!

    Can you please elaborate a bit more how can I apply manacher. I tried using it but I am unable to cover the case that "substrings considered may not be distinct". Since manacher uses the results of already processed string.

    • »
      »
      »
      7 лет назад, # ^ |
        Проголосовать: нравится +7 Проголосовать: не нравится

      Manacher's algorithm tells you the length of the longest palindrome substring for every center. So if maximum length palindrome has length x, than we can also make palindromes with length x - 2k (where x - 2k > 0). And using this we can maintain two prefix sums — one for odd lengths and one for even lengths. When we need to answer query we just get sum of both prefix sums in appropriate range.

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

    will you please provide your code, for the above problem.

»
7 лет назад, # |
Rev. 4   Проголосовать: нравится -12 Проголосовать: не нравится

Upd: Doesn't work as karanbatra explains.

»
7 лет назад, # |
  Проголосовать: нравится -15 Проголосовать: не нравится

Hint:The number of distinct palindromes in a string of length n can't exceed n