给定一个长度为 n 的整数序列 a1,a2,…,an 以及一个长度为 m 的整数序列 b1,b2,…,bm。
请你判断 a 序列是否为 b 序列的子序列。
子序列指序列的一部分项按原有次序排列而得的序列,例如序列 {a1,a3,a5} 是序列 {a1,a2,a3,a4,a5} 的一个子序列。
输入格式
第一行包含两个整数 n,m。
第二行包含 n 个整数,表示 a1,a2,…,an。
第三行包含 m 个整数,表示 b1,b2,…,bm。
输出格式
如果 a 序列是 b 序列的子序列,输出一行 Yes。
否则,输出 No。
数据范围
1≤n≤m≤105,
−109≤ai,bi≤109
输入样例:
3 5
1 3 5
1 2 3 4 5
输出样例:
Yes
https://www.acwing.com/problem/content/2818/
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
int a[n+1],b[m+1];
for(int i=0;i<n;i++) cin>>a[i];
for(int j=0;j<m;j++) cin>>b[j];
int i=0, j=0;
while(i<n&&j<m) {
if(a[i]==b[j]) i++;
j++;
}
if(i==n) cout<<"Yes\n";
else cout<<"No\n";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct A {
int l;
int r;
};
struct f {
bool operator() (const A& a, const A& b) {
if(a.l<b.l) return true;
else return false;
}
};
int main() {
int n;
cin>>n;
A a[n+2];
for(int i=0; i<n; i++) cin>>a[i].l>>a[i].r;
sort(a,a+n,f());
// cout<<"a:\n";
// for(int i=0; i<n; i++) {
// printf("%d %d\n",a[i].l,a[i].r);
// }
// int index=0;
// int ans=1;
// int st=a[index].l;
// int ed=a[index].r;
//
// while(index<n-1) {
//
printf("\n\na[index+1] = a[%d] l = %d, r = %d\n\n",index+1,a[index+1].l,a[index+1].r);
//
// if(ed>=a[index+1].l) {
// if(ed<a[index+1].r)
// ed=a[index+1].r;
printf("\n\ntrue:\ned = %d\n",ed);
// } else {
// ans++;
// st=a[index+1].l;
// ed=a[index+1].r;
printf("\n\nflase:\nans = %d\nst = %d\ned = %d\n",ans,st,ed);
// }
// index++;
// }
// cout<<ans<<endl;
int cnt=1;
int nowl=a[0].l,nowr=a[0].r;
for(int i=1;i<n;i++) {
if(a[i].l<=nowr){
nowr=max(nowr,a[i].r);
}else{
nowr=a[i].r;
cnt++;
}
nowl=a[i].l;
}
cout<<cnt<<endl;
return 0;
}