A Hard Problem
InterviewTime limit2sMemory limit1024 MB
Given two binary sequences, find the length of the longest common subsequence that is non-decreasing.
- Level
Medium5 of 10
- Topics
- String, Prefix sum, Dynamic programming
- Solved
- No attempts yet
Statement
To escape from the game, Doctor Smolder Braveston has to solve a hard problem. You are given two sequences consisting of zeros and ones. Find the maximum length of a sequence that is a subsequence of both and is non-decreasing. The doctor is much better at throwing boomerangs than at solving problems like this, so he asked you for help.
Input
The first line contains a single integer , the length of the first sequence (). The second line contains integers , the elements of the first sequence (). The third line contains a single integer , the length of the second sequence (). The fourth line contains integers , the elements of the second sequence ().
Output
Print a single integer, the length of the longest common non-decreasing subsequence of the two sequences.
Hint
In the example from the statement, the longest common non-decreasing subsequence is {0, 0, 1, 1, 1}, which has length 5.