CodeForces - 1005D ----Polycarp and Div 3(这居然是道思维题!!!)

点击这里带你去mod3

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
.

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 3.

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

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

 

今天的我依旧有测试,依旧是dp专题,不得不说 我们这个学长真的坏,居然把这道题挂在了第一个???上来看了一眼,好像好复杂的样子,这会是个什么dp呢?反正来者不善,没有思路。。。就把这道题给扔了

直到后来旁边的大佬告诉我这是一道思维题,代码很短,我竟无言以对。。。。

题目意思是给你一个字符串,字符串由数字组成,让你在数字之间插入隔板,使得隔板间的连续数字尽量是三的倍数,问你最多能分成多少个三的倍数?

这里有两种思路,一个是找规律,另一个是尺取法

先说说第一个找规律吧:

1)我们先枚举他的每一个数字,如果是3的倍数(模3=0)则res++

2)否则考虑两个数字,前两个数字对3取模可以得到以下结果[1,1][1,2][2,1][2,2],
很明显中间两项可以被三整除,因为他们余数和恰好为3  res++

3)如果两个数字的时候是[1,1][2,2]这两种情况,我们再考虑第三个数字,
如果第三个数字直接可以被三整除那么我们舍弃前两个,只要第三个就可以了,是一样的。    
如果第三个数不是3的倍数,则对于前两个数的和对3取余,结果为[1,1]或者[2,2]
(如果为[1,2],[2,1],则这两个数字能够被3整除)对于第三个数对3取余,结果为0,1,2:
0:第三个数本身能被3整除res++
1:[1,1,1]是3的倍数取全部,[2,2,1]取后两个   res++
2:[1,1,2]取后两个 [2,2,2]是3的倍数,取全部  res++
所以 对于n=3 一定可以找到

这个还是比较烧脑的,反正我的脑子是不够用,,,,

我们再说一下另一种做法吧     尺取法:

就是一段一段的去取,取到三的倍数即res++,这个说起来好理解吧,就是循环多点 写起来有点绕,但是挺好想的

下面奉上两种解法的AC代码

找规律版

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=2e5+5;

char s[mm];
int a[mm];
int res=0;
int mod;
int n;
int sum;
int main()
{
	scanf("%s",s);
	int len=strlen(s);
	for(int i=0;i<len;i++){
		a[i]=s[i]-'0';
		mod=a[i]%3;
		sum+=mod;
  		n++;
  		if(mod==0||sum%3==0||n==3){
  		//直接是三的倍数,凑起来是三的倍数,规律养成 
   			res++;
  			n=sum=0;//归位 
		}
	}
	printf("%d\n",res);
	return 0;
}

尺取法版:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=2e5+5;
char s[mm];
int a[mm];
int res=0;
int temp;
int tt;
int main()
{
	scanf("%s",s);
	int len=strlen(s);
	
	for(int i=0;i<len;i++){
		a[i]=s[i]-'0';
		if(a[i]%3==0){
			res++;
			temp=i+1;// 
		}
		else {
			for(int j=temp;j<i;j++){
				tt=0;
				for(int k=j;k<=i;k++){
					tt+=a[k];//各位数字的和  
					if(tt%3==0){
						res++;
						temp=k+1;
					}
				}
			}
		}
	}	
	printf("%d\n",res);
	return 0;
}

 

唉,太卑微了   Orz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值