当前位置:网站首页>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;
}
边栏推荐
- 【周赛复盘】LeetCode第80场双周赛
- [weekly replay] game 80 of leetcode
- Learning record [email protected] understand canvas
- Fiddler packet capturing (mobile app)
- Task fruit Juicer 0611
- Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
- RTOS rt-thread裸机系统与多线程系统
- IDEA中文棱形乱码错误解决方法--控制台中文输出棱形乱码
- < 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
- Project training of Software College of Shandong University rendering engine system point cloud processing (10)
猜你喜欢
![[practical case of light source] UV-LED curing innovation makes the production line more smooth](/img/6f/04f52a37782c54b1050f908f46eadf.png)
[practical case of light source] UV-LED curing innovation makes the production line more smooth

C语言 分割bin文件程序

What is JUC in high concurrency programming

【工具推荐】个人本地 markdown 知识图谱软件 Obsidian
![Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]](/img/ee/ae9984355a98f5529dd4574eea2e7c.jpg)
Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]

Idea Encyclopedia (Reprinted)

Analysis on the development status and direction of China's cultural tourism real estate industry in 2021: the average transaction price has increased, and cultural tourism projects continue to innova

IGMP message (tcp/ip details volume 1/ Volume 2)

Applet: how to get the user's mobile number in the plug-in

聊聊事件监听那些事-上
随机推荐
鼻孔插灯,智商上升,风靡硅谷,3万就成
Introduction and download website of common data of GIS, remote sensing, hydrology and Geography (2), supplementary~
What is JUC in high concurrency programming
华为设备配置CE双归属
5-5配置Mysql复制 基于日志点的复制
Import and export steps of SQL Server 2008
Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)
Chapter I linear table
< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)
When programming is included in the college entrance examination...
Apache kylin Adventure
联通网管协议框图
Explore the Apache shardingsphere SQL parse format function
Step by step to create a trial version of ABAP program containing custom screen
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
借助SpotBugs将程序错误扼杀在摇篮中
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)
puppeteer入门之 BrowserContext 类
Learning record [email protected] understand canvas
Why doesn't Alibaba recommend MySQL use the text type?