Codeforces 1382A

A. Common Subsequence

time limit per test1 second
memory limit per test256 megabytes
input standard input
output standard output
You are given two arrays of integers a1,…,an and b1,…,bm.

Your task is to find a non-empty array c1,…,ck that is a subsequence of a1,…,an, and also a subsequence of b1,…,bm. 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 is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero) elements. For example, [3,1] is a subsequence of [3,2,1] and [4,3,1], but not a subsequence of [1,3,3,7] and [3,10,4].

Input

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

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

The second line of each test case contains n integers a1,…,an (1≤ai≤1000) — the elements of the first array.

The third line of each test case contains m integers b1,…,bm (1≤bi≤1000) — the elements of the second array.

It is guaranteed that the sum of n and the sum of m across all test cases does not exceed 1000 (∑i=1tni,∑i=1tmi≤1000).

Output

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 (1≤k≤1000) — the length of the array, followed by k integers c1,…,ck (1≤ci≤1000) — the elements of the array.

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

Example
input
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
output
YES
1 4
YES
1 3
NO
YES
1 3
YES
1 2
Note
In the first test case, [4] is a subsequence of [10,8,6,4] and [1,2,3,4,5]. This array has length 1, it is the smallest possible length of a subsequence of both a and b.

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

题目大意:

一共有t组样例,每组给出两个数组,询问是否存在这两个数组中都有的序列,如果存在则打印YES,并找到长度最小的公共序列,打印长度并打印元素。不存在则打印NO。

解题思路:

大水题。标记一下A数组出现过哪些数,如果在B中找到了某一个则直接输出长度为1,并输出这个数,如果找不到则输出NO。AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e3 + 50;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int a[N];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		memset(a, 0 ,sizeof a);
		int n, n1, ans;
		cin >> n >> n1;
		bool ok = false;
		for (int i = 1; i <= n; i ++)
		{
			int s;
			cin >> s;
			a[s] = 1;
		}
		for (int i = 1; i <= n1; i ++)
		{
			int s;
			cin >> s;
			if (a[s])
			{
				ok = true;
				ans = s;
			}
		}
		if (ok)
		  cout  << "YES" << endl << 1 << " " << ans << endl;
		else
		  cout << "NO" << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Hayasaka/p/14294190.html