P27968 [洛谷] [ROIR 2022 Day 1] Palindrome Array
提高-
题目描述

Problem Background

Translated from ROIR 2022 D1T4.

There are two arrays of natural numbers, $A = [a_1, a_2, \dots, a_n]$ and $B = [b_1, b_2, \dots, b_m]$.

For each array, a prefix and a suffix, which may be empty, are randomly removed such that the remaining parts of the arrays are of equal length. We denote the resulting arrays as $A'$ and $B'$, each of length $k$. Then, the corresponding elements of these arrays are summed, resulting in an array $C = [c_1, c_2, \dots, c_k]$.

For example, suppose $n = 5, A = [4, 3, 3, 2, 1], m = 6, B = [4, 1, 5, 1, 3, 2]$. Removing the first and last elements from array $A$ and the first three elements from array $B$ results in $A' = [3, 3, 2], B' = [1, 3, 2]$, and their corresponding elements summed result in $C = [4, 6, 4]$.
Suppose $n = 7, A = [1, 9, 1, 9, 8, 1, 0], m = 6, B = [1, 1, 4, 5, 1, 4]$. Removing the first two elements and the last element from array $A$ and the first and last elements from array $B$ results in $A' = [1, 9, 8, 1], B' = [1, 4, 5, 1]$, and their corresponding elements summed result in $C = [2, 13, 13, 2]$.

Problem Description

Find the maximum length $k$ of the array $C$ that can be a palindrome.

Input Format

The first line contains two integers $n$ and $m$, representing the number of elements in the first and second arrays, respectively ($1 \le n, m \le 100,000$).

The second line contains $n$ integers $a_1, a_2, \dots, a_n$, representing array $A$ ($1 \le a_i \le 100$).

The third line contains $m$ integers $b_1, b_2, \dots, b_m$, representing array $B$ ($1 \le b_i \le 100$).

Output Format

Output a single integer, the maximum length $k$ of the palindrome array $C$.

Sample Input and Output

Sample Input #1

5 6
4 3 3 2 1
4 1 5 1 3 2

Sample Output #1

3

Sample Input #2

7 6
1 9 1 9 8 1 0
1 1 4 5 1 4

Sample Output #2

4

Notes/Hints

This problem uses bundled testing.

Subtask Points Special Conditions
1 13 $n, m \le 300$
2 33 All elements of array B are equal
3 16 $n \le 500, m \le 10^5$
4 38 None

For all data, $1 \le n, m \le 100,000$, $1 \le a_i \le 100$, $1 \le b_i \le 100$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 6 4 3 3 2 1 4 1 5 1 3 2
样例输出
3