Palindrome Partition (Codeforces 1828E)
Tags: DP, hashing

A substring is a continuous and non-empty segment of letters from a given string, without any reorders.An even palindrome is a string that reads the same backward as forward and has an even length. For example, strings "zz", "abba", "abccba" are even palindromes, but strings "codeforces", "reality", "aba", "c" are not.A beautiful string is an even palindrome or a string that can be partitioned into some smaller even palindromes.You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. Count the number of beautiful substrings of $$$s$$$.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 5\cdot 10^5$$$).The second line of each test case contains a string $$$s$$$. String $$$s$$$ consists of only lowercase Latin letters and has a length of $$$n$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5\cdot 10^5$$$.

Output: For each test case print the number of beautiful substrings.

Note: In the first test case, the beautiful substrings are "abaaba", "baab", "aa".In the last test case, the beautiful substrings are "aa" (counted twice), "abba", "bb", "bbaa", "abbaaa".

Sample Inputs:
6
6
abaaba
1
a
2
aa
6
abcdef
12
accabccbacca
6
abbaaa

Sample Outputs:
3
0
1
0
14
6

Solution (Python):
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def manacher(s):
    L = ['*']
    n = len(s)
    for i in range(n):
        L.append(s[i])
        L.append('*')
    n = 2 * n + 1
    j = 0
    ini = 1
    final = [0] * n
    while j < n:
        for k in range(ini, min(j + 1, n - j)):
            if L[j - k] == L[j + k]:
                final[j] += 1
            else:
                break
        if final[j] != 0:
            for kk in range(1, final[j] + 1):
                if final[j - kk] < final[j] - kk:
                    final[j + kk] = final[j - kk]
                else:
                    break
            final[j + kk] = final[j] - kk
            ini = final[j] - kk + 1
            j += kk
        else:
            j += 1
            ini = 1
    return final

def t():
    n = int(input())
    s = input()
    X = manacher(s)
    dp1 = [0] * n
    st = []
    for i in range(n - 1, -1, -1):
        while st and st[-1] - X[2 * st[-1]] // 2 > i:
            del st[-1]
        if st:
            dp1[i] = 2 * (st[-1] - i)
        st.append(i)
    dp2 = [0] * (n + 1)
    for j in range(n - 1, -1, -1):
        if dp1[j] != 0:
            dp2[j] = dp2[j + dp1[j]] + 1
    print(sum(dp2))
if __name__ == '__main__':
    for _ in range(int(input())):
        t()

Solution (Python):
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def manacher(s):
    L = ['*']
    n = len(s)
    for i in range(n):
        L.append(s[i])
        L.append('*')
    n = 2 * n + 1
    j = 0
    ini = 1
    final = [0] * n
    while j < n:
        for k in range(ini, min(j + 1, n - j)):
            if L[j - k] == L[j + k]:
                final[j] += 1
            else:
                break
        if final[j] != 0:
            for kk in range(1, final[j] + 1):
                if final[j - kk] < final[j] - kk:
                    final[j + kk] = final[j - kk]
                else:
                    break
            final[j + kk] = final[j] - kk
            ini = final[j] - kk + 1
            j += kk
        else:
            j += 1
            ini = 1
    return final

def t():
    n = int(input())
    s = input()
    X = manacher(s)
    dp1 = [0] * n
    st = []
    for i in range(n - 1, -1, -1):
        while st and st[-1] - X[2 * st[-1]] // 2 > i:
            del st[-1]
        if st:
            dp1[i] = 2 * (st[-1] - i)
        st.append(i)
    dp2 = [0] * (n + 1)
    for j in range(n - 1, -1, -1):
        if dp1[j] != 0:
            dp2[j] = dp2[j + dp1[j]] + 1
    sys.stdout.write(str(sum(dp2)) + '\n')
if __name__ == '__main__':
    for _ in range(int(input())):
        t()

Solution (Python):
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def manacher(s):
    L = ['*']
    n = len(s)
    for i in range(n):
        L.append(s[i])
        L.append('*')
    n = 2 * n + 1
    j = 0
    ini = 1
    final = [0] * n
    while j < n:
        for k in range(ini, min(j + 1, n - j)):
            if L[j - k] == L[j + k]:
                final[j] += 1
            else:
                break
        if final[j] != 0:
            for kk in range(1, final[j] + 1):
                if final[j - kk] < final[j] - kk:
                    final[j + kk] = final[j - kk]
                else:
                    break
            final[j + kk] = final[j] - kk
            ini = final[j] - kk + 1
            j += kk
        else:
            j += 1
            ini = 1
    return final

def t():
    n = int(input())
    s = input()
    X = manacher(s)
    dp1 = [0] * n
    st = []
    for i in range(n - 1, -1, -1):
        while st and st[-1] - X[2 * st[-1]] // 2 > i:
            del st[-1]
        if st:
            dp1[i] = 2 * (st[-1] - i)
        st.append(i)
    dp2 = [0] * (n + 1)
    for j in range(n - 1, -1, -1):
        if dp1[j] != 0:
            dp2[j] = dp2[j + dp1[j]] + 1
    sys.stdout.write(str(sum(dp2)) + '\n')
if __name__ == '__main__':
    for _ in range(int(input())):
        t()