Polycarp and Div 3 (CodeForces - 1005D)

欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

Description

Polycarp likes numbers that are divisible by 3.

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

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

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 33 that Polycarp can obtain?

Input

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

Output

Print the maximum number of numbers divisible by 33 that Polycarp can get by making vertical cuts in the given number ss.

Examples

Input

3121

Output

2

Input

6

Output

1

Input

1000000000000000000000000000000000

Output

33

Input

201920181

Output

4

Note

In the first example, an example set of optimal cuts on the number is 3|1|21.

In the second example, you do not need to make any cuts. The specified number 6 forms one number that is divisible by 33.

扫描二维码关注公众号,回复: 2556015 查看本文章

In the third example, cuts must be made between each pair of digits. As a result, Polycarp gets one digit 1 and 3333 digits 0. Each of the 3333 digits 0 forms a number that is divisible by 33.

In the fourth example, an example set of optimal cuts is 2|0|1|9|201|81. The numbers 00, 99, 201201 and 8181are divisible by 33.

题意:

给一个仅包含数字的字符串,将字符串分割成多个片段(无前导0),求这些片段里最多能分成多少个3的倍数。

思路一:

如样例2,数值可能比较大,于是为方便处理我们用字符串输入a[ ],再用每一位减去0的ASCII码值(48)来转化成int型存进b[ ],并定义k记录拆出的3的倍数的数量,此外,我们还需要定义一个变量xb来记录第一位非3倍数的数,初始化为0。因为题中让求的是最多能拆成多少个能被3整除的数,所以我们尽可能将每一位自己本身就是3的倍数的数单独拆出,于是对于每一位我们都先判断该为是否为3的倍数,若是直接k++,并将下标更新为i+1(i+1才可能不是3的倍数)。大家都知道:若各位的和能被3整除,则这个数是3的倍数。于是我们只需要求xb到当前i之间有没有区间使得加和为3的倍数即可。

代码一:

#include<cstdio>
#include<cstring>

int main()
{
    char a[200010];
    int b[200010],i,j,n,l,m=1,xb=0,k=0;
    scanf("%s",a);
    n=strlen(a);
    for(i=0;i<=n-1;i++)
    {
        b[i]=a[i]-48;
        if(b[i]%3==0)
        {
            k++;
            xb=i+1;
        }
        else
        {
            for(j=xb;j<i;j++)
            {
                m=0;
                for(l=j;l<=i;l++)
                {
                    m+=b[l];
                    if(m%3==0)
                    {
                        xb=l+1;
                        k++;
                    }
                }
            }
        }
    }
    printf("%d\n",k);
    return 0;
}

思路二:

对于单独的一个数字,如果是3的倍数,则k++,否则,考虑连续的两个数字,若是则k++,如果第三个数本身是3的倍数,也k++,如果第三个数不是3的倍数,则对于前两个数的和对3取余,结果为[1,1]或者[2,2](如果为[1,2],[2,1],则这两个数字能够被3整除)。对于第三个数对3取余,结果为0,1,2

0:第三个数本身能被3整除k++;

1:[1,1,1]是3的倍数取全部,[2,2,1]取后两个k++;

2:[1,1,2]取后两个 [2,2,2]是3的倍数,取全部k++;

所以 对于n=3 一定可以找到。

代码二:

#include<cstdio>
#include<cstring>

using namespace std;

int main()
{
    char str[200010];
    int z=0,sum=0,k=0,n=0;
    scanf("%s",str);
    for(int i=0; i<strlen(str); i++)
    {
        z=(str[i]-'0')%3;
        sum+=z;
        n++;
        if(z==0||sum%3==0||n==3)
        {
            k++;
            n=sum=0;
        }
    }
    printf("%d\n",k);
    return 0;
}

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

猜你喜欢

转载自blog.csdn.net/lxt_Lucia/article/details/81295566