Longest Contiguous Subsequence

No attempts yetTime limit1sMemory limit128 MB

Problem

You are given two integer sequences $S1$ and $S2$. $S1$ has length $L_1$ ($1 \le L_1 \le 180$) and $S2$ has length $L_2$ ($1 \le L_2 \le 180$). Print the length of the longest contiguous subsequence of numbers common to both $S1$ and $S2$.

The elements of $S1$ are $S1_1, S1_2, \dots, S1_{L_1}$ ($-100 \le S1_i \le 100$), and the elements of $S2$ are $S2_1, S2_2, \dots, S2_{L_2}$ ($-100 \le S2_i \le 100$).

A contiguous subsequence is a consecutive run of numbers in the sequence. For example, the contiguous subsequences of 1 2 3 1 are: the empty sequence, 1, 1 2, 1 2 3, 1 2 3 1, 2, 2 3, 2 3 1, 3, 3 1, and a second occurrence of 1.

This is a typical problem that people solve early in their competitive programming career.

Input

  • Line 1: Two space-separated integers $L_1$ and $L_2$
  • Next $L_1$ lines: each line contains a single integer $S1_i$
  • Next $L_2$ lines: each line contains a single integer $S2_i$

Output

  • A single integer: the length of the longest contiguous subsequence common to $S1$ and $S2$

Hint

In the sample, the answer $7$ corresponds to the common contiguous subsequence 1, 1, 1, 3, 2, 3, 3.