CodeForces - 1005D

CodeForces - 1005D

Polycarp likes numbers that are divisible by 3.

He has a huge number s. Polycarp wants to cut from it the maximum number of numbers that are divisible by 3. To do this, he makes an arbitrary number of vertical cuts between pairs of adjacent digits. As a result, after m such cuts, there will be m+1 parts in total. Polycarp analyzes each of the obtained numbers and finds the number of those that are divisible by 3.

For example, if the original number is s=3121, then Polycarp can cut it into three parts with two cuts: 3|1|21. As a result, he will get two numbers that are divisible by 3.

Polycarp can make an arbitrary number of vertical cuts, where each cut is made between a pair of adjacent digits. The resulting numbers cannot contain extra leading zeroes (that is, the number can begin with 0 if and only if this number is exactly one character '0'). For example, 007, 01 and 00099 are not valid numbers, but 90, 0 and 10001 are valid.

What is the maximum number of numbers divisible by 3 that Polycarp can obtain?

Input

The first line of the input contains a positive integer s. The number of digits of the number s is between 1 and 2⋅105, inclusive. The first (leftmost) digit is not equal to 0.

Output

Print the maximum number of numbers divisible by 3 that Polycarp can get by making vertical cuts in the given number s

题意

给出一个长度不超过2e5的字符串,将其进行划分,输出能被3整除的数字的个数。

思路

若当前数字能被3整除直接计数,如果不能,存起来,继续判断下一位,当数字能被3整除或者超过了100,计数。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    int ans=0;
    int sum=0;
    cin>>a;
    int len = a.size();
    for(int i=0;i<len;i++)
    {
        if((a[i]-'0')%3==0)
        {
            ans++;
            sum=0;   
            continue;
        }
        sum*=10;
        sum+=a[i]-'0';
        if(sum%3==0||sum>100)
        {
            ans++;    
            sum=0;
            continue;
        }
    }
    cout<<ans<<endl;
    return 0;
}
posted @ 2018-07-30 09:42  cifiyoo  阅读(293)  评论(0编辑  收藏  举报