#C14491. Longest Common Subarray
Longest Common Subarray
Longest Common Subarray
Given two arrays of integers, you are required to determine the length of the longest common contiguous subarray (subarray) shared by both arrays.
Let us denote the first array as \(A\) and the second array as \(B\). You need to find a contiguous sequence \(A[i..i+k-1]\) and \(B[j..j+k-1]\) such that \(A[l] = B[l']\) for all corresponding positions in the subarray and \(k\) is maximized. One typical dynamic programming relation is given by:
\[ dp[i][j] = \begin{cases} dp[i+1][j+1] + 1, & \text{if } A[i] = B[j] \\ 0, & \text{otherwise} \end{cases} \]
Your task is to compute and output the maximum value of \(k\) found.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains an integer \(m\), representing the length of the first array.
- The second line contains \(m\) space-separated integers forming the first array.
- The third line contains an integer \(n\), representing the length of the second array.
- The fourth line contains \(n\) space-separated integers forming the second array.
outputFormat
Output a single integer to standard output (stdout), which is the length of the longest common contiguous subarray.
## sample5
1 2 3 2 1
5
3 2 1 4 7
3