#7041. [ICPC 2024 Seoul R] Palindromic Length

[ICPC 2024 Seoul R] Palindromic Length

[ICPC 2024 Seoul R] Palindromic Length

题目描述

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 $ of length $ n $ can be measured by its **palindromic length**, $ \text{PL}(S) $, which is the minimum number of palindromic substrings into which $ S $ can be partitioned. More precisely, $ \text{PL}(S) $ is the minimum number $ t $ ($ 1 \leq t \leq n $) such that there exist palindromic substrings $ S_1, S_2, \dots, S_t $ whose concatenation $ S_1S_2 \cdots S_t $ becomes $ S $. To make it easier to distinguish, we denote a partition of $ S $ into $ S_1, S_2, \dots, S_t $ as $ 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 aba∣aca \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=aca∣b∣a S = \text{aca} \mid \text{b} \mid \text{a} or S=a∣c∣aba 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) .

输入格式

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

输出格式

Your program is to write to standard output. Print exactly one line. The line should contain a positive integer which is the palindromic length $ \text{PL}(S) $ of the input string $ S $.
6
abaaca
2
5
acaba
3
5
abcde
5
5
radar
1

提示

(暂无提示) 标签: P14771|2024|回文自动机 PAM|ICPC|首尔

来源

P14771|[ICPC 2024 Seoul R] Palindromic Length