题目描述
Problem Description
There are many cards with an integer between 1 and 1000 written on them. Anna and Bruno play a game using these cards as follows:
Anna has a pile of A cards, and Bruno has a pile of B cards. Anna discards any number of cards (even zero) from her A cards to form a new pile. Bruno discards any number of cards (even zero) from the top and the bottom of his B cards to form a new pile, without rearranging the remaining cards. If the two new piles match, the number of cards in one pile becomes the score for both. Two piles match if they contain the same number of cards n, and the integer on the i-th card from the top (1 ≤ i ≤ n) is the same for both piles.
For example, if Anna has a pile of 5 cards with integers 1, 2, 3, 4, 5 from top to bottom, and Bruno has a pile of 4 cards with integers 3, 1, 4, 1 from top to bottom. If Anna discards cards 2, 3, and 5, and Bruno discards the top card 3 and the bottom card 1, both piles will match. In this case, the number of cards in the remaining pile is 2, so both get a score of 2.
We want to find the maximum score for both players.
Input Format
Read the following data from standard input:
The first line contains two integers A and B separated by a space.
The second line contains A integers separated by spaces, where the i-th integer (1 ≤ i ≤ A) represents the integer on the i-th card from the top of Anna's pile.
The third line contains B integers separated by spaces, where the j-th integer (1 ≤ j ≤ B) represents the integer on the j-th card from the top of Bruno's pile.
Output Format
Output the maximum score as an integer on one line to standard output.
Sample Input and Output
Sample Input #1
5 4
1 2 3 4 5
3 1 4 1
Sample Output #1
2
Sample Input #2
6 5
4 1 5 2 3 4
4 5 4 2 3
Sample Output #2
3
Notes/Hints
Task
Create a program that, given the information about the piles of cards Anna and Bruno have, finds the maximum score for both players.
Constraints
$ 1 ≤ A ≤ 5000 $
$ 1 ≤ B ≤ 5000 $
The integers on the cards are between 1 and 1000.
Grading Criteria
For 10% of the grading data, $ A ≤ 10 $ and $ B ≤ 10 $.
For 50% of the grading data, $ A ≤ 100 $ and $ B ≤ 100 $.
Sample Explanation 1
This input/output example corresponds to the example in the problem statement.
Sample Explanation 2
In this input/output example, there are two ways for both players to score 3 points. If Anna discards cards 1, 2, and 3, and Bruno discards cards 2 and 3, both piles will have the integers 4, 5, 4 from top to bottom, resulting in a score of 3. Alternatively, if Anna discards cards 1, 5, and 4, and Bruno discards the top card 4 and the bottom card 5, both piles will have the integers 4, 2, 3 from top to bottom, also resulting in a score of 3.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分