#P14804. [CCPC 2024 哈尔滨站] 一个朴素的字符串问题

[CCPC 2024 哈尔滨站] 一个朴素的字符串问题

Description

You are given a character grid with 22 rows and nn columns, where each cell contains a lowercase letter. You can start at any position in the grid and move several steps, with each step either to the right or downward, stopping at any cell. Concatenating the characters from the cells visited in order forms a string.

A string SS is called a double string if and only if there exists a non-empty string TT such that S=TTS = TT. For example, aa\texttt{aa} and xyzxyz\texttt{xyzxyz} are double strings, while a\texttt{a} and xyzyz\texttt{xyzyz} are not.

Given the character grid, find the length of the longest double string you can obtain.

Input Format

The first line contains an integer nn (1n2×1051 \le n \le 2 \times 10^5), representing the number of columns in the character grid.

The next two lines contain two strings of length nn consisting only of lowercase English letters, representing the character grid.

Output Format

Output a single integer, representing the length of the longest double string you can obtain.

5
abcab
acabc
6
6
babbaa
babaaa
6
2
ne
fu
0

Hint

In the first example, the longest double string can be obtained as follows (not unique):

$$\begin{aligned} \underline{\texttt{abc}}\texttt{ab}\\ \texttt{ac}\underline{\texttt{abc}} \end{aligned}$$