CodeForces - 1005D - Polycarp and Div 3(思维题)

CodeForces - 1005D - Polycarp and Div 3(思维题)

题目链接:http://codeforces.com/problemset/problem/1005/D
Polycarp and Div 3
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 11 and 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.

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 8181 are divisible by 3

像这么长的英文题,真的很让人头疼,特别是像作者这样英语超弱的理工男。
题目大意是:给你一串由数字组成的字符串(最长2e5),要你把它分割成一些数字,使得在这些数字中能被3整除的个数最多。
解法:考虑将每个数字模3以后的结果。
这个题目主要是个思维题目,如果当前数字能够直接被3整除(0, 3, 6, 9)的话,那就直接结果加一就好了。
上面是只判断一位数字的情况,那么再就是两位数字组合了,假设xy吧,那么不是上一中情况的话说明肯定有x%3,y%3都不等于0,并且只能等于1或者2。要使这两个数组合能被三整除的话,必须取模之后必须加和等于3。假设x%3==1,y%3==2.那么两个数组合的值就是m = x*10+y = (3*k+1)*10+(3*l+2)。那么m/3 = (10+2)/3 = 12/3 = 4是能整除的。
还有3个的情况,假设xyz,那么相邻的不能加和为3,所以只能相邻的数字取模相同,就只有两种情况,取模都为2或1。证明同上。
所以总结一下有三种情况:
1.当前数字模3为0;
2.现有的数字之和模3为0(当前正在处理的);
3.有三个数字了一定可以做到模3为0。
然后一个一个数字处理即可。
借鉴的文章:https://blog.csdn.net/sinat_37158899/article/details/80994056

AC代码

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 2e5+5;

char str[maxn];

int main()
{
    gets(str);
    int ans = 0, sum = 0, n = 0;
    for(int i = 0; str[i] != '\0'; i++)
    {
        int x = str[i] - '0';
        sum += x;//计算当前的和
        n++;//处理的数字+1
        if(n == 3 || sum % 3 == 0 || x % 3 == 0)//如果已经有3个数字或者sum模3为0或者当前数字是3的倍数,n和sum都置0,则个数加一
        {
            sum = n = 0;
            ans++;
        }

    }
    printf("%d\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/81270132
今日推荐