题目:
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 6forms 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 numbers00, 99, 201201 and 8181 are divisible by 33.
题意:
给你一个超级大的数,让你把这个数进行分割,然后尽量分隔出来能被3整除的数,让你求最多能够分割出来的数;
思路:
这道题算是有一个坑吧,最容易死在第11组数据上;
对于数据能够被3整除,那么这个数的每一位数相加也能够被3整除,
如果第一位数能够被3整除,那么这个数能够被3整除;
接下来考虑下一位数,
如果第二位数和第一位数的和能够被3整除,那么这个二位数也能够被3整除;否则考虑第3位数,
由第一二位数我们可以知道,第一位数的余数应该是0,1,2,第一二位数的余数应该是0,1,2;
当余数为0时,结果+1,所以不符合条件的余数组合就是(1,1,x)和(2,2,x);
关于第3位数的余数,我们可以发现无论第3位数的余数为几,结果都是可以被3整除的;
例如:
被第1位的余数和前两位的余数进行过判定后就剩下了这几种情况;
(1,1,1):3位和能被3整除;
(1,1,2):后两位和能被3整除;
(2,2,1):后两位和能被3整除;
(2,2,2):后三位和能被3整除;
也就是说,剩下的这几种情况都是可以被3整除的,就等于只要判断3位数,就有一种情况在我们的判断之外并且可以被3整除,所以代码中就有了k==3时,sum++;这一条;
for(i=0;i<l;i++)
{
t=(a[i]-'0')%3;
s+=a[i]-'0';
k++;
if(t==0||s%3==0||k==3)
{
sum++;
k=0;
s=0;
}
}
例如:
221;
那么我们可以知道,第1位,前2位和前3位 不可以被3整除,但是后两位可以分割出来被3整除,所以这就是我们容易忽略的地方;
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[200010];
int main()
{
while(~scanf("%s",a))
{
int l=strlen(a);
int i,t,s=0,sum=0,k=0;
for(i=0;i<l;i++)
{
t=(a[i]-'0')%3;//判断第一位数;
s+=a[i]-'0';//判断前两位数;
k++;//记录是第几位数;
if(t==0||s%3==0||k==3)//k==3,代表每判断3位数就会用一个肯定会被3整除的数字出现;
{
sum++;
k=0;//还原;
s=0;
}
}
printf("%d\n",sum);
}
return 0;
}