A. Common Subsequence ( Codeforces Round #658 (Div. 2))
程序员文章站
2022-04-30 22:48:32
...
思路:这道题就是给你两个数组a和b,你要去找b中的元素是否在a中存在。所以我们直接用到了map<int,int>mp。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t; cin>>t;
while(t--){
int ans = -1;
int a,b,n,m;
map<int,int>mp;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a; mp[a]++;
}
for(int i=0;i<m;i++){
cin>>b;
if(mp[b]>0){
ans = b ;
}
}
if(ans == -1) cout<<"NO"<<endl;
else {
{ cout<<"YES"<<endl<<1<<' '<<ans<<endl;}
}
}
return 0;
}
python写法:
for _ in range(int(input())):
n,m = map(int,input().split())
lis1 = list(map(int,input().split()))
lis2 = list(map(int,input().split()))
check = 0
ans=1
for i in lis1:
if i in lis2:
check =1
ans=i
break
if check == 1:
print ("YES")
print (1,ans)
else:
print ("NO")
推荐阅读
-
Codeforces Round #655 (Div. 2) A. Omkar and Completion
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #659 (Div. 2) A. Common Prefixes(字符串,思维)
-
Codeforces Round #658 (Div. 2)C1. Prefix Flip (Easy Version)(贪心)
-
Codeforces Round #658 (Div. 2) (C1、C2)
-
Codeforces Round #648 (Div. 2) E、Maximum Subsequence Value
-
Codeforces Round #658 (Div. 2) B. Sequential Nim
-
Codeforces Round #658 (Div. 2) D. Unmerge(dp,01背包)
-
Educational Codeforces Round 93 (Rated for Div. 2) A. Bad Triangle
-
Codeforces Round #664 (Div. 2) A. Boboniu Likes to Color Balls