아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Longest Common Subsequence

시간 제한2초메모리 제한256 MB

요약
값이 1, 2, 3뿐인 두 수열이 주어질 때, 비감소 조건을 만족하는 가장 긴 공통 부분 수열의 길이를 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 그리디, 배열, 조합론
정답자
아직 제출이 없습니다

문제

Chiaki has two sequences a_1,a_2,…,a_na\_1,a\_2,\ldots,a\_n and b_1,b_2,…,b_mb\_1,b\_2,\ldots,b\_m. She would like to find their longest common subsequence c_1,c_2,…,c_kc\_1,c\_2,\ldots,c\_k such that c_1≤c_2≤…≤c_kc\_1 \le c\_2 \le \ldots \le c\_k.

입력

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1≤n,m≤1061 \le n, m \le 10^6): the lengths of two sequences.

The second line contains nn integers a_1,a_2,…,a_na\_1,a\_2,\ldots,a\_n (1≤a_i≤31 \le a\_i \le 3).

The third line contains mm integers b_1,b_2,…,b_mb\_1,b\_2,\ldots,b\_m (1≤b_i≤31 \le b\_i \le 3).

It is guaranteed that the sum of max⁡n,m\max\\{n,m\\} in all test cases does not exceed 10610^6.

출력

For each test case, output a single integer kk: the length of the longest common subsequence c_1,c_2,…,c_kc\_1,c\_2,\ldots,c\_k such that c_1≤c_2≤…≤c_kc\_1 \le c\_2 \le \ldots \le c\_k.

예제1

  1. 예제 1

    입력
    3
    3 3
    1 2 3
    1 2 3
    3 3
    1 1 1
    1 1 2
    3 3
    1 3 2
    1 2 3
    
    예상 출력
    3
    2
    2