当前位置:网站首页>acwing 2816. 判断子序列
acwing 2816. 判断子序列
2022-06-12 16:08:00 【_刘小雨】
给定一个长度为 n 的整数序列 a1,a2,…,an 以及一个长度为 m 的整数序列 b1,b2,…,bm。
请你判断 a 序列是否为 b 序列的子序列。
子序列指序列的一部分项按原有次序排列而得的序列,例如序列 {a1,a3,a5} 是序列 {a1,a2,a3,a4,a5} 的一个子序列。
输入格式
第一行包含两个整数 n,m。
第二行包含 n 个整数,表示 a1,a2,…,an。
第三行包含 m 个整数,表示 b1,b2,…,bm。
输出格式
如果 a 序列是 b 序列的子序列,输出一行 Yes。
否则,输出 No。
数据范围
1≤n≤m≤105,
−109≤ai,bi≤109
输入样例:
3 5
1 3 5
1 2 3 4 5
输出样例:
Yes
双指针算法: 很容易想出来。
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;
}
或者这样写
#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;
}
边栏推荐
- 2022.02.28 - SX11-05. The largest rectangle in the histogram
- Scanpy (VI) analysis and visualization of spatial transcriptome data
- 学习记录[email protected]一文搞懂canvas
- < 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(五)
- 【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
- Servlet knowledge explanation (2)
- Global and Chinese markets of automatic glue applicators 2022-2028: Research Report on technology, participants, trends, market size and share
- Getting started with JMeter
- 借助SpotBugs将程序错误扼杀在摇篮中
- Great God cracked the AMD k6-2+ processor 22 years ago and opened the hidden 128KB L2 cache
猜你喜欢
The difference between TCP and UDP, the three handshakes of TCP and the four waves of TCP
C语言 分割bin文件程序
redis String类型常见命令
5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band
鼻孔插灯,智商上升,风靡硅谷,3万就成
借助SpotBugs将程序错误扼杀在摇篮中
Defer learning in golang
The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence
Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
随机推荐
Redis General Command
5g new scheme! Upgrade the existing base station and UE simulator to 5g millimeter wave band
【架构优化过程思考】如何构建技术方案影响的评估能力
Global and Chinese market for material injection 2022-2028: Research Report on technology, participants, trends, market size and share
(四)GoogleNet複現
Keep an IT training diary 067- good people are grateful, bad people are right
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
Import and export steps of SQL Server 2008
Application of postman-rest client plug-in
Scanpy(六)空间转录组数据的分析与可视化
Development practice of ag1280q48 in domestic CPLD
从斐波那契数列求和想到的俗手、本手和妙手
Step by step steps to create an ABAP program with a custom screen
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]
puppeteer入门之 Page 类
[thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
当编程纳入到高考。。。
< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)
< 山东大学软件学院项目实训 > 渲染引擎系统——点云处理(十)
Understanding of dart typedef