欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

B.Last Year‘s Substring

程序员文章站 2022-05-21 08:13:57
...

https://codeforces.ml/contest/1462/problem/B

题意就是判断一串字符串中,经过n次删除一段连续的字符串的操作之后,使字符串变为”2020“,如果n<=1,输出”YES“,否则输出"NO"。观察一下就能发现可行的情况无非就是以下五种:

  1. XXX2020
  2. 2XXX020
  3. 20XXX20
  4. 202XXX0
  5. 2020XXX

想得到的话,就能A了…

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		string s;
		cin >> n;
		cin >> s;
		if (s[0] == '2' && s[1] == '0' && s[2] == '2' && s[3] == '0')
			cout << "YES" << endl;
		else if (s[0] == '2' && s[n - 3] == '0' && s[n - 2] == '2' && s[n - 1] == '0')
			cout << "YES" << endl;
		else if (s[0] == '2' && s[1] == '0' && s[n - 2] == '2' && s[n - 1] == '0')
			cout << "YES" << endl;
		else if (s[0] == '2' && s[1] == '0' && s[2] == '2' && s[n - 1] == '0')
			cout << "YES" << endl;
		else if (s[n - 4] == '2' && s[n - 3] == '0' && s[n - 2] == '2' && s[n - 1] == '0')
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}