2020(2020强智杯)

题目链接:https://ac.nowcoder.com/acm/contest/9699/A

题目描述:

Bobo有一个只包含数字 012 的,长度为 n 的字符串 s1 ... sn。他想选出最多的互不重叠的连续子串,这些子串都是2020。求最多可以选出的子串数量。
形式化的,他想求出最大的 k ,是的存在 k 个下标 i1,...,ik 满足:
  .  Sit​Sit+1​Sit+2​Sit+3=2020

  .  对于1≤t<k1\leq t<k1t<k,满足it+4≤it+1i_{t}+4\leq i_{t+1}it+4it+1
 

 

输入描述:

输入文件包含多组数据,请处理到文件结束。
每组数据的第一行包括一个整数 n ,第二行包括一个字符串s1 ... sn。
 · 1 ≤ n ≤ 105
 · si ∈ {0, 1, 2}
 · n 的和不超过106

输出描述:

对于每组数据,输出一个整数,表示所求的值

 

示例:

输入:
4
2020
6
202020
10
1202012020

输出
1
1
2

 

题目分析:

无,纯水

 

代码

Python3.9:

import sys

while True:
    a = 0
    ans = 0
    line = sys.stdin.readline()
    if not line:
        break
    b = input()
    c = b[0:int(line)]
    while a < int(line) - 3:
        if c[a] == '2' and c[a + 1] == '0' and c[a + 2] == '2' and c[a + 3] == '0':
            ans = ans + 1
            a = a + 3
        a+=1
    print(ans)

 

C++:

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int num,ans = 0;
    vector<char>a;
    char b; 
    while(cin>>num)
    {
        for(int x = 0;x<num;x++)
        {
            cin>>b;
            a.push_back(b);
        }
        if(num<4)
        {
            cout<<ans;
            return 0;
        }
        for(int z = 0;z<num-3;z++)
        {
            if(a[z]=='2'&&a[z+1]=='0'&&a[z+2]=='2'&&a[z+3]=='0')
            {
                ans++;   
                z+=3;
            }
        } 
        cout<<ans<<endl;
        a.clear();
        ans = 0;
    }
}

 

posted @ 2021-01-14 11:32  Carrout  阅读(128)  评论(0编辑  收藏  举报