#P4324. Longest Twisted Palindrome

    ID: 17570 Type: Default 1000ms 256MiB

Longest Twisted Palindrome

Longest Twisted Palindrome

You are given two strings (A) and (B) of equal length (N). A twisted string (S(i,j,k)) is defined as the concatenation of the substring from the (i)th character to the (j)th character in (A) and the substring from the (j)th character to the (k)th character in (B) (both indices are 1-indexed). Formally, (S(i,j,k)=A[i\ldots j]\Vert B[j\ldots k]). For example, if (A=\mathtt{XYZ}) and (B=\mathtt{UVW}) then (S(1,2,3)=\mathtt{XYVW}).

A twisted palindrome is defined as one of the following:

  1. A palindromic substring of (A).
  2. A palindromic substring of (B).
  3. A twisted string (S(i,j,k)) which is a palindrome.

Your task is to find the length of the longest twisted palindrome among all possibilities.

A string is a palindrome if it reads the same backward as forward. (N) is the length of each string.

inputFormat

The first line contains an integer (N), the length of the strings.\nThe second line contains the string (A).\nThe third line contains the string (B).

outputFormat

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

sample

3
XYZ
UVW
1

</p>