Compressed LCS
시간 제한3초메모리 제한512 MB
런 렝스로 압축된 두 정수 수열이 주어질 때, 두 수열의 최장 공통 부분 수열 길이를 구한다.
문제
Bobo has two integer sequences and , both in compressed form. means that begins with copies of the integer , followed by copies of the integer , copies of the integer , and so on. is of similar format.
Bobo would like to find the LCS (longest common subsequence) for and . Recall that sequence is a subsequence of if and only if can be obtained by deleting some (maybe all, maybe none) elements from .
입력
The input contains zero or more test cases, and is terminated by end-of-file. For each test case:
The first line contains two integers and ().
The -th of the following lines contains two integers and . And the -th of the last lines contains two integers and .
The constraints are: , , .
It is guaranteed that the sum of and the sum of both do not exceed .
출력
For each test case, output an integer which denotes the length of the LCS.