《判断子序列》(双指针做法)

42 阅读1分钟

双指针

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 998244353;
const int N = 100010;
int a[N], b[N];
void solve() {
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i ++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i ++) {
		cin >> b[i];
	}
	int i = 0, j = 0;//定义两个指针,分别遍历两个数组
	while (i < n && j < m) {
		if (a[i] == b[j]) {//因为两个数组都是单调升序的
			i ++;
		}
		j ++;
	}
	if (i == n) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int _ = 1;
	int t;
	cin >> t;
	while (t --) {
		solve();
	}
	return 0;
}