原题链接: https://codeforces.com/contest/1382/problem/A
测试样例
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, 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”.
题意: 给你两个数组,求他们的最短公共子序列,若没有,则输出NO。
解题思路: 妥妥的水题,最短就为1,再短就不存在,所以只要判断有无公共元素即可。具体看代码。(PS:比赛一定要先把题过一遍,因为你不知道哪一题是签到题。)
AC代码
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t;
int n,m;
int a[maxn],b[maxn];
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
map<int,int> p;
cin>>n>>m;
rep(i,0,n-1){
cin>>a[i];
p[a[i]]++;
}
bool flag=false;
int result;
rep(i,0,m-1){
cin>>b[i];
if(flag==false&&p.count(b[i])>0){
result=b[i];
flag=true;
}
}
if(flag){
cout<<"YES"<<endl;
cout<<1<<" "<<result<<endl;
}
else{
cout<<"NO"<<endl;
}
}
}
return 0;
}