判断子序列

题目 判断子序列

image-5dc100dd

思路分析

image-eee07d42

长数组的指针一直往后走 短数组的匹配到了才走

代码实现

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'

const int N=100010;
int a[N],b[N];
int n,m;

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    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++;
    }
*/
    int i=0;
    for(int j=0;j<m;j++){
        if(i<n && a[i]==b[j])
            i++;
    }

    if(i==n)
        printf("Yes");
    else
        printf("No");
    return 0;
}

同类题型

视频讲解

📹 配套视频讲解:判断子序列(acwing 视频课,原网址 Trilium 导出时未保留,待回填)


⬅️ 数组元素的目标和 🏠 00-听课板子 ➡️ 位运算