判断子序列()-其他
判断子序列()
给定一个长度为 nn 的整数序列 a1,a2,…,an以及一个长度为 m 的整数序列 b1,b2,…,bm。
请你判断 a 序列是否为 b 序列的子序列。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int n, m;
int a[N], b[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for (int i = 0; i < m; i ++ ) scanf("%d", &b[i]);
for (int i = 0, j = 0; j < m; ) {
if (a[i] == b[j]) {
i++, j++;
if (i == n) {
puts("Yes");
return 0;
}
}
else j++;
}
puts("No");
return 0;
}
————————
给定一个长度为 nn 的整数序列 a1,a2,…,an以及一个长度为 m 的整数序列 b1,b2,…,bm。
请你判断 a 序列是否为 b 序列的子序列。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100010;
int n, m;
int a[N], b[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
for (int i = 0; i < m; i ++ ) scanf("%d", &b[i]);
for (int i = 0, j = 0; j < m; ) {
if (a[i] == b[j]) {
i++, j++;
if (i == n) {
puts("Yes");
return 0;
}
}
else j++;
}
puts("No");
return 0;
}