I was trying this problem from Gym Contest. I don’t know why I am getting WA on Test 2. Here’s my code : Link.

I have used Hashing for checking number of palindromes and then Segment Tree to find the index for each query. Can anyone point out whats wrong in my code or give an alternate solution.

Thanks

why hashing?since length<30,u could just use an O(length^2) algorithm(stack)

edit:sorry not stack,dp

dp[i][j] denotes if substring s[i:j] is a palindrome

dp[i][j]=true if dp[i+1][[j-1] is true and s[i]==s[j]

I tried with what you said… Picked code from GFG xD. But it still shows WA

Here’s the link : (https://ideone.com/mipoDA)

Am I doing something wrong still?

It magically works now!!

Thanks Vivek for suggesting alternate approach, but can you please point out where I am going wrong in Hashing?

hashing does have countercases where it fails(collisions)

But I don’t think hashing can have collisions here as 4^{30} fits in long long( around 1.4 * 10^{18} ).

Ur hash function is sum(4^i*(s[i]-‘a’+1)) if i am not wrong

One countercase(collision) is:

string gd,ce

both strings have hash value 23

7+4*4=3+4*5=23

Only ‘a’, ‘b’ and ‘c’ are allowed as letters

OOh sorry!! I didnt read the question properly