Longest Contiguous Subsequence
InterviewTime limit1sMemory limit128 MB
Given two integer sequences, find the length of the longest run of consecutive elements that appears in both.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array, Brute force, Implementation
- Solved
- No attempts yet
Problem
You are given two integer sequences and . has length () and has length (). Print the length of the longest contiguous subsequence of numbers common to both and .
The elements of are (), and the elements of are ().
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 and
- Next lines: each line contains a single integer
- Next lines: each line contains a single integer
Output
- A single integer: the length of the longest contiguous subsequence common to and
Hint
In the sample, the answer corresponds to the common contiguous subsequence 1, 1, 1, 3, 2, 3, 3.