#P14771. [ICPC 2024 Seoul R] Palindromic Length

[ICPC 2024 Seoul R] Palindromic Length

Description

A string is called a palindrome if it is read the same forward and backward. Palindromes are useful factors for measuring the complexity of strings like the asymmetry of the strings. The asymmetry of a string S S of length n n can be measured by its palindromic length, PL(S) \text{PL}(S) , which is the minimum number of palindromic substrings into which S S can be partitioned. More precisely, PL(S) \text{PL}(S) is the minimum number t t (1tn 1 \leq t \leq n ) such that there exist palindromic substrings S1,S2,,St S_1, S_2, \dots, S_t whose concatenation S1S2St S_1S_2 \cdots S_t becomes S S . To make it easier to distinguish, we denote a partition of S S into S1,S2,,St S_1, S_2, \dots, S_t as S1S2St S_1 \mid S_2 \mid \cdots \mid S_t .

For example, a string S=abaaca S = \text{abaaca} can be partitioned into two palindromic substrings as abaaca \text{aba} \mid \text{aca} , that is the minimum, so PL(abaaca)=2 \text{PL}(\text{abaaca}) = 2 . A string acaba \text{acaba} cannot be partitioned into two palindromic substrings, but it can be partitioned into three palindromic substrings, S=acaba S = \text{aca} \mid \text{b} \mid \text{a} or S=acaba S = \text{a} \mid \text{c} \mid \text{aba} , so PL(acaba)=3 \text{PL}(\text{acaba}) = 3 . For S=radar S = \text{radar} , PL(S)=1 \text{PL}(S) = 1 because S S is a palindrome. PL(S)=5 \text{PL}(S) = 5 for S=abcde S = \text{abcde} .

Given a non-empty string S S of English lowercase letters, write a program to output PL(S) \text{PL}(S) .

Input Format

Your program is to read from standard input. The input starts with a line containing a positive integer n n (1n100,000 1 \leq n \leq 100,000 ), where n n is the number of letters of a string. The next line contains a string of n n English lowercase letters. Note that the string contains no space between the letters.

Output Format

Your program is to write to standard output. Print exactly one line. The line should contain a positive integer which is the palindromic length PL(S) \text{PL}(S) of the input string S S .

6
abaaca
2
5
acaba
3
5
abcde
5
5
radar
1