当前位置:网站首页>Codeforces Round #690 (Div. 3) B. Last Year‘s Substring 常规解法
Codeforces Round #690 (Div. 3) B. Last Year‘s Substring 常规解法
2022-07-21 15:05:00 【Is_TuTouYa】
原题如下
Polycarp has a string s[1…n] of length n consisting of decimal digits. Polycarp performs the following operation with the string s no more than once (i.e. he can perform operation 0 or 1 time):
Polycarp selects two numbers i and j (1≤i≤j≤n) and removes characters from the s string at the positions i,i+1,i+2,…,j (i.e. removes substring s[i…j]). More formally, Polycarp turns the string s into the string s1s2…si−1sj+1sj+2…sn.
For example, the string s=“20192020” Polycarp can turn into strings:
“2020” (in this case (i,j)=(3,6) or (i,j)=(1,4));
“2019220” (in this case (i,j)=(6,6));
“020” (in this case (i,j)=(1,5));
other operations are also possible, only a few of them are listed above.
Polycarp likes the string “2020” very much, so he is wondering if it is possible to turn the string s into a string “2020” in no more than one operation? Note that you can perform zero operations.
Input
The first line contains a positive integer t (1≤t≤1000) — number of test cases in the test. Then t test cases follow.
The first line of each test case contains an integer n (4≤n≤200) — length of the string s. The next line contains a string s of length n consisting of decimal digits. It is allowed that the string s starts with digit 0.
Output
For each test case, output on a separate line:
“YES” if Polycarp can turn the string s into a string “2020” in no more than one operation (i.e. he can perform 0 or 1 operation);
“NO” otherwise.
You may print every letter of “YES” and “NO” in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
input
6
8
20192020
8
22019020
4
2020
5
20002
6
729040
6
200200
output
YES
YES
YES
NO
NO
NO
题目大意
给你一个字符串,问是否能进行不超过一次的操作(删除从i至j的字符)是字符串变为“2020”,如果可以输出“YES”,否则输出"NO"。
由于只能进行一次或者0次操作,说明该字符串的首尾必须符合一下六种类型:
- 2020
- 2020XXXXX
- 2XXXXX020
- 20XXXXX20
- 202XXXXX0
- XXXXX2020
在知道这么个规律之后,我们便可直接用if语句解决这个问题
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
const int N = 1e4+10;
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 0;i < n;i++){
char a[N];
int x;
cin >> x; //字符串的长度
for(int j = 0;j < x;j++) cin >> a[j]; //输入字符串
if(a[x-1]=='0'&&a[x-2]=='2'&&a[x-3]=='0'&&a[x-4]=='2')
cout << "YES" << endl;
else if(a[x-1] == '0'&&a[x-2]=='2'&&a[x-3]=='0'&&a[0]=='2')
cout << "YES" << endl;
else if(a[x-1] == '0'&&a[x-2]=='2'&&a[1]=='0'&&a[0]=='2')
cout << "YES" << endl;
else if(a[x-1] == '0'&&a[2]=='2'&&a[1]=='0'&&a[0]=='2')
cout << "YES" << endl;
else if(a[3] == '0'&&a[2]=='2'&&a[1]=='0'&&a[0]=='2')
cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
看懂了吗,看不懂也得懂(doge)
END
边栏推荐
猜你喜欢
logisim计组实验十 单周期MIPS CPU
直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
Lodash中数组和对象合并方法assign、merge、defaults、defaultsDeep比较
运行时,物体移动旋转缩放插件,“RuntimeTransformGizmos插件”使用教程(Unity3D)
Keithley software 2400 | 2440 | 2450 | 2460 | 2461 | 2470 ns SourceMeter source table software
jsp自定义标签库--foreach
AT&T伪指令 以及对于CFI CFA的解释
笔记本电脑选购指南(不推荐具体品牌及型号)
查找表中重复内容
echart的markArea遇到的坑啊为什么不是按照我设置的时间来显示的区间呢
随机推荐
Keithley software 2600 series 2601b | 2602b | 2604b | 2606b ns SourceMeter source table software
【数学基础】 foundation of mathematics :Jensen不等式
PS Xiaobai starts from 0
课题六 - 消息队列实现客户机通信
ps小白從0開始……
地理卫星数据下载
unity GetAxis(string axisName); How did the axisname value come from
容器与容器 & 容器与主机 - 通过ssh协议互联(多节点、跨主机)
echart的markArea遇到的坑啊为什么不是按照我设置的时间来显示的区间呢
Mod17a3hgf data download
During the attack and defense drill | know Chuangyu Zhao Wei: let the security ability grow on the cloud
QT图形界面初学者项目 - 无人机群作战仿真模拟
Set MySQL time zone
Join hands with HMS core analysis services to help the efficient growth of games with data
Unity 3D modify default editor
QSqlDatabase: QMYSQL driver not loaded的解决方案
C语言基础练习题
[MySQL and database] MySQL & database Chapter 8: variables in MySQL
ENVI shp转roi并对栅格进行掩膜提取
[MySQL and database] MySQL & database Chapter 7: view learning