#P4324. [JSOI2016] 扭动的回文串

    ID: 3254 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp2016江苏枚举,暴力

[JSOI2016] 扭动的回文串

Description

JYY has two strings AA and BB, both of length NN.

A "twisted string" S(i,j,k)S(i,j,k) is formed by concatenating the substring of AA from the ii-th character to the jj-th character and the substring of BB from the jj-th character to the kk-th character.

For example, if A=XYZA=\mathtt{XYZ} and B=UVWB=\mathtt{UVW}, then the twisted string S(1,2,3)=XYVWS(1,2,3)=\mathtt{XYVW}.

JYY defines a "twisted palindrome" as one of the following:

  • A palindrome in AA;
  • A palindrome in BB;
  • Or a twisted string S(i,j,k)S(i,j,k) that is a palindrome.

Now JYY wants to find the longest twisted palindrome.

Input Format

The first line contains a positive integer NN. The second line contains a string AA of length NN consisting of uppercase letters. The third line contains a string BB of length NN consisting of uppercase letters.

Output Format

Output a single integer on the first line, the length of the longest twisted palindrome.

5
ABCDE
BAECB
5

Hint

Sample explanation: The twisted palindrome in the best solution is shown below (characters not in the palindrome are marked with .):

.BC..
..ECB

Constraints: For all testdata, 1N1051 \leq N \leq 10^5.

Translated by ChatGPT 5