欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1382A.Common Subsequence
Common Subsequence
You are given two arrays of integers and .
Your task is to find a non-empty array that is a subsequence of , and also a subsequence of . 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 is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero) elements. For example, is a subsequence of and , but not a subsequence of and .
Input
The first line contains a single integer () — the number of test cases. Next lines contain descriptions of test cases.
The first line of each test case contains two integers and () — the lengths of the two arrays.
The second line of each test case contains integers () — the elements of the first array.
The third line of each test case contains integers () — the elements of the second array.
It is guaranteed that the sum of and the sum of across all test cases does not exceed ($um\limits_{i=1}^t n_i, um\limits_{i=1}^t m_i\le 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 () — the length of the array, followed by integers () — the elements of the array.
If there are multiple solutions with the smallest possible , output any.
Note
In the first test case, is a subsequence of and . This array has length , it is the smallest possible length of a subsequence of both and .
In the third test case, no non-empty subsequences of both and exist, so the answer is "NO".
Samples
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
YES
1 4
YES
1 3
NO
YES
1 3
YES
1 2
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |