双指针算法 AcWing 2816. 判断子序列

双指针算法 AcWing 2816. 判断子序列

原题链接

AcWing 2816. 判断子序列

算法标签

双指针

思路

子序列指序列的一部分项按原有次序排列而得的序列。
在枚举序列a时, 若匹配上, 即a[i]==b[j],需使i, j后移, 若尚未匹配上,需使j后移。所以j满足单调性, 可使用双指针算法

代码

#include
#define int long long
#define rep(i, a, b) for(int i=a;ib;--i)
using namespace std;
const int N = 100005;
int a[N];
// 数组a[i]中每个数子出现的新次数
int b[N];
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
void put(int x) {
    if(x<0) putchar('-'),x=-x;
    if(x>=10) put(x/10);
    putchar(x%10^48);
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n=read(), m=read();
    rep(i, 0, n){
        a[i]=read();
    }
    rep(i, 0, m){
        b[i]=read();
    }
    int i=0, j=0;
    while(i

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
双指针算法 AcWing 2816. 判断子序列_第1张图片

你可能感兴趣的:(算法,算法,c++,数据结构)