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

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

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
  • Проголосовать: не нравится

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

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

But also by DP here

»
8 лет назад, скрыть # |
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

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

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

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

Upd: Doesn't work as karanbatra explains.

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

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