#Q1382A. Common Subsequence

Common Subsequence

Common Subsequence

题面翻译

给出两个数字个数分别为n,mn,m的整数数组a,ba,b。 你需要找出一个最短的整数数组cc使得它同时是a,ba,b的子序列。如果可以,请输出"YES",并输出cc的数字个数及整个cc数组;否则,输出"NO"。

题目描述

You are given two arrays of integers a1,,an a_1,\ldots,a_n and b1,,bm b_1,\ldots,b_m .

Your task is to find a non-empty array c1,,ck c_1,\ldots,c_k that is a subsequence of a1,,an a_1,\ldots,a_n , and also a subsequence of b1,,bm b_1,\ldots,b_m . If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it.

A sequence a a is a subsequence of a sequence b b if a a can be obtained from b b by deletion of several (possibly, zero) elements. For example, [3,1] [3,1] is a subsequence of [3,2,1] [3,2,1] and [4,3,1] [4,3,1] , but not a subsequence of [1,3,3,7] [1,3,3,7] and [3,10,4] [3,10,4] .

输入格式

The first line contains a single integer t t ( 1t1000 1\le t\le 1000 ) — the number of test cases. Next 3t 3t lines contain descriptions of test cases.

The first line of each test case contains two integers n n and m m ( 1n,m1000 1\le n,m\le 1000 ) — the lengths of the two arrays.

The second line of each test case contains n n integers a1,,an a_1,\ldots,a_n ( 1ai1000 1\le a_i\le 1000 ) — the elements of the first array.

The third line of each test case contains m m integers b1,,bm b_1,\ldots,b_m ( 1bi1000 1\le b_i\le 1000 ) — the elements of the second array.

It is guaranteed that the sum of n n and the sum of m m across all test cases does not exceed 1000 1000 ( i=1tni,i=1tmi1000 \sum\limits_{i=1}^t n_i, \sum\limits_{i=1}^t m_i\le 1000 ).

输出格式

For each test case, output "YES" if a solution exists, or "NO" otherwise.

If the answer is "YES", on the next line output an integer k k ( 1k1000 1\le k\le 1000 ) — the length of the array, followed by k k integers c1,,ck c_1,\ldots,c_k ( 1ci1000 1\le c_i\le 1000 ) — the elements of the array.

If there are multiple solutions with the smallest possible k k , output any.

样例 #1

样例输入 #1

5
4 5
10 8 6 4
1 2 3 4 5
1 1
3
3
1 1
3
2
5 3
1000 2 2 2 3
3 1 5
5 5
1 2 3 4 5
1 2 3 4 5

样例输出 #1

YES
1 4
YES
1 3
NO
YES
1 3
YES
1 2

提示

In the first test case, [4] [4] is a subsequence of [10,8,6,4] [10, 8, 6, 4] and [1,2,3,4,5] [1, 2, 3, 4, 5] . This array has length 1 1 , it is the smallest possible length of a subsequence of both a a and b b .

In the third test case, no non-empty subsequences of both [3] [3] and [2] [2] exist, so the answer is "NO".