CF1382A.Common Subsequence

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two arrays of integers a1,…,ana_1,\ldots,a_n and b1,…,bmb_1,\ldots,b_m.

Your task is to find a non-empty array c1,…,ckc_1,\ldots,c_k that is a subsequence of a1,…,ana_1,\ldots,a_n, and also a subsequence of b1,…,bmb_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 aa is a subsequence of a sequence bb if aa can be obtained from bb 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].

给你两个整数数组 a1,…,ana_1,\ldots,a_n 和 b1,…,bmb_1,\ldots,b_m。

你的任务是找出一个非空数组 c1,…,ckc_1,\ldots,c_k,它既是 a1,…,ana_1,\ldots,a_n 的子序列,也是 b1,…,bmb_1,\ldots,b_m 的子序列。如果存在多个满足条件的数组,请找出其中长度最小的一个;如果仍存在多个长度最小的数组,则任选其一即可。如果不存在这样的数组,请报告该情况。

序列 aa 是序列 bb 的子序列,当且仅当 aa 可通过从 bb 中删除若干(可能为零)个元素得到。例如,[3,1][3,1] 是 [3,2,1][3,2,1] 和 [4,3,1][4,3,1] 的子序列,但不是 [1,3,3,7][1,3,3,7] 和 [3,10,4][3,10,4] 的子序列。

输入格式

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

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

The second line of each test case contains nn integers a1,…,ana_1,\ldots,a_n (1≤ai≤10001\le a_i\le 1000) — the elements of the first array.

The third line of each test case contains mm integers b1,…,bmb_1,\ldots,b_m (1≤bi≤10001\le b_i\le 1000) — the elements of the second array.

It is guaranteed that the sum of nn and the sum of mm across all test cases does not exceed 10001000 (∑i=1tni,∑i=1tmi≤1000\sum\limits_{i=1}^t n_i, \sum\limits_{i=1}^t m_i\le 1000).

第一行包含一个整数 tt(1≤t≤10001\le t\le 1000),表示测试用例的数量。接下来的 3t3t 行包含各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤10001\le n,m\le 1000),分别表示两个数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,…,ana_1,\ldots,a_n(1≤ai≤10001\le a_i\le 1000),表示第一个数组的元素。

每个测试用例的第三行包含 mm 个整数 b1,…,bmb_1,\ldots,b_m(1≤bi≤10001\le b_i\le 1000),表示第二个数组的元素。

保证所有测试用例中 nn 的总和与 mm 的总和均不超过 10001000(即 ∑i=1tni,∑i=1tmi≤1000\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 kk (1≤k≤10001\le k\le 1000) — the length of the array, followed by kk integers c1,…,ckc_1,\ldots,c_k (1≤ci≤10001\le c_i\le 1000) — the elements of the array.

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

对于每个测试用例,如果存在解,则输出 "YES";否则输出 "NO"。

如果答案为 "YES",则在下一行输出一个整数 kk(1≤k≤10001\le k\le 1000)—— 表示数组的长度,随后输出 kk 个整数 c1,…,ckc_1,\ldots,c_k(1≤ci≤10001\le c_i\le 1000)—— 表示该数组的元素。

如果存在多个满足最小可能 kk 的解,输出任意一个即可。

输入输出样例

  • 输入#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 11, it is the smallest possible length of a subsequence of both aa and bb.

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

在第一个测试用例中,[4][4] 是 [10,8,6,4][10, 8, 6, 4] 和 [1,2,3,4,5][1, 2, 3, 4, 5] 的公共子序列。该数组长度为 11,是 aa 和 bb 的公共子序列的最小可能长度。

在第三个测试用例中,[3][3] 和 [2][2] 不存在任何非空公共子序列,因此答案为 “NO”。

输入解题思路,AI测评打分。不知道怎么写?

首页