AcWing.2816判断子序列
用双指针
code:
#include<bits/stdc++.h> using namespace std; const int N = 1e5+5; int n,m,a[N],b[N]; int read() { int f=1,ans=0;char c=getchar(); while(c>‘9‘||c<‘0‘)if(c==‘-‘){f*=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){ ans=(ans<<3)+(ans<<1)+c-‘0‘; c=getchar(); } return f*ans; } int main() { n=read();m=read(); for(int i=1;i<=n;++i)a[i]=read(); for(int i=1;i<=m;++i)b[i]=read(); int i=1,j=1; while(i<=n&&j<=m) { while(j<=m&&a[i]!=b[j])j++; if(a[i]==b[j])i++,j++; } if(i>n)puts("Yes"); else puts("No"); return 0; }
原文:https://www.cnblogs.com/xfldbk/p/14128865.html