题目链接:https://codeforces.com/contest/1462
B. Last Year’s Substring
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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).
题目大意
给一个字符串,问能否最多删除连续一段使得剩下的字符串是“2020”?
思路:相当于有一个隔板。枚举隔板在2020之间的位置就可以了。
把隔板表示成#
1.2020#
2.202#0
3.20#20
4.2#020
5.#2020;
其他的就是NO
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N=210;
char s[N];
int t,n;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
cin>>s+1;
if(s[1]=='2'&&s[2]=='0'&&s[3]=='2'&&s[4]=='0') cout<<"yes"<<endl;
else if(s[1]=='2'&&s[2]=='0'&&s[3]=='2'&&s[n]=='0') cout<<"yes"<<endl;
else if(s[1]=='2'&&s[2]=='0'&&s[n-1]=='2'&&s[n]=='0') cout<<"yes"<<endl;
else if(s[1]=='2'&&s[n-2]=='0'&&s[n-1]=='2'&&s[n]=='0') cout<<"yes"<<endl;
else if(s[n-3]=='2'&&s[n-2]=='0'&&s[n-1]=='2'&&s[n]=='0') cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}