B.Last Year‘s Substring
程序员文章站
2022-05-21 08:13:57
...
题意就是判断一串字符串中,经过n次删除一段连续的字符串的操作之后,使字符串变为”2020“,如果n<=1,输出”YES“,否则输出"NO"。观察一下就能发现可行的情况无非就是以下五种:
- XXX2020
- 2XXX020
- 20XXX20
- 202XXX0
- 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;
}
下一篇: 员工的重要性(力扣:690)