首页 > 其他分享 >2816. 判断子序列(双指针模板题)

2816. 判断子序列(双指针模板题)

时间:2023-03-14 21:11:28浏览次数:50  
标签:2816 int ++ 序列 模板 指针

https://www.acwing.com/problem/content/2818/
双指针模板题:
i指针只有匹配到相等才++,
j指针无论如何每次都++
那么i==n时,意味着b序列中存在着a序列,且有序离散存放

#include<iostream>
using namespace std;
const int N = 1e5+10;
int n,m,a[N],b[N];
int main()
{
    cin >> n >> m;
    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" << endl;
    else cout << "No" << endl;
    return 0;
}

 

标签:2816,int,++,序列,模板,指针
From: https://www.cnblogs.com/lxl-233/p/17216394.html

相关文章