A. Common Subsequence(水题)Codeforces Round #658 (Div. 2)
原题链接: 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, [ 4 ] [4] [4] is a subsequence of [ 10 , 8 , 6 , 4 ] [10, 8, 6, 4] [10,8,6,4] and [ 1 , 2 , 3 , 4 , 5 ] [1, 2, 3, 4, 5] [1,2,3,4,5]. This array has length 1 1 1, it is the smallest possible length of a subsequence of both a a a and b b b.
In the third test case, no non-empty subsequences of both [ 3 ] [3] [3] and [ 2 ] [2] [2] exist, so the answer is “NO”.
题意: 给你两个数组,求他们的最短公共子序列,若没有,则输出NO。
解题思路: 妥妥的水题,最短就为1,再短就不存在,所以只要判断有无公共元素即可。具体看代码。(PS:比赛一定要先把题过一遍,因为你不知道哪一题是签到题。)
AC代码
/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h> //POJ不支持
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)