当前位置:网站首页>acwing 2816. Judgement subsequence
acwing 2816. Judgement subsequence
2022-06-12 16:18:00 【_ Liuxiaoyu】
Given a length of n Integer sequence of a1,a2,…,an And a length of m Integer sequence of b1,b2,…,bm.
Please judge a Whether the sequence is b Subsequences of sequences .
Subsequence refers to the sequence obtained by arranging some items of the sequence in the original order , For example, the sequence {a1,a3,a5} It's a sequence {a1,a2,a3,a4,a5} A subsequence of .
Input format
The first line contains two integers n,m.
The second line contains n It's an integer , Express a1,a2,…,an.
The third line contains m It's an integer , Express b1,b2,…,bm.
Output format
If a Sequence is b Subsequences of sequences , Output one line Yes.
otherwise , Output No.
Data range
1≤n≤m≤105,
−109≤ai,bi≤109
sample input :
3 5
1 3 5
1 2 3 4 5
sample output :
Yes
Double pointer algorithm : It's easy to think of .
code:
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int a[N], b[N];
int main()
{
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";
else cout << "No";
return 0;
}
Or write like this
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int a[N], b[N];
int main()
{
cin >> n >> m;
for(int i =0 ;i < n; i ++) cin >> a[i];
for(int i =0; i < m; i ++) cin >> b[i];
int j = 0;
for(int i = 0; i < m && j < n; i ++)
{
if(a[j] == b[i]) j ++;
}
//cout << j << endl;
if(j == n ) cout << "Yes";
else cout << "No";
return 0;
}
边栏推荐
猜你喜欢

办公室VR黄片,骚操作!微软HoloLens之父辞职!

acwing 801. 二进制中1的个数(位运算)

Sum of acwing796 submatrix

【工具推荐】个人本地 markdown 知识图谱软件 Obsidian

同源?跨域?如何解决跨域?

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(六)

Why doesn't Alibaba recommend MySQL use the text type?

Office VR porn, coquettish operation! The father of Microsoft hololens resigns!

鼻孔插灯,智商上升,风靡硅谷,3万就成

Batch --03---cmdutil
随机推荐
The C Programming Language(第 2 版) 笔记 / 7 输入与输出 / 7.8 其它函数
UE4 common type conversion
一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
借助SpotBugs将程序错误扼杀在摇篮中
Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)
< 山东大学软件学院项目实训 > 渲染引擎系统——点云处理(十)
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
Interview: why do integer wrapper classes try to use equals() to compare sizes
glibc 内存管理模型 释放 C库内存缓存
大规模实时分位数计算——Quantile Sketches 简史
Saga体系结构模式:微服务架构下跨服务事务的实现
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
联通网管协议框图
C regular expression
面试:了解装箱和拆箱操作吗?
聊聊事件监听那些事-上
看《梦华录》上头的人都该尝试下这款抖音特效
办公室VR黄片,骚操作!微软HoloLens之父辞职!
Scanpy (VI) analysis and visualization of spatial transcriptome data
国产CPLD中AG1280Q48进行开发的实践之一:思路分析