Regionals 2014 >> Latin America



题意:找出多少个子串满足以下条件:是数字且能被3整除

思路:本状态根据前一状态退出,本状态的这个数字取余后加上原来状态会变成下一个状态。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <vector>
using namespace std;

typedef long long ll;
#define MAX 20005
#define PI acos(-1.0)
bool isdigit(char c){
	if(c>='0' && c<='9') return true;
	return false;
}
char str[1000005]; 
int f[1000005];
int main(){
	while(~scanf("%s", str)){
		int len = strlen(str);
		ll ans = 0;
		int then = -1;
		int flag = 0;
		ll t1 = 0,t2 = 0,t3 = 0;
		for(int i = 0;i < len;i++){
			if(isdigit(str[i])){
				int t = (str[i]-'0')%3;
				ll tt1 = 0,tt2 = 0,tt3 = 0;
				if(t == 0){
				//	t1 ++;
					tt1 = t1,tt2 = t2,tt3 = t3;
					tt1 ++; //noice
				}
				else if(t == 1){
				//	t2 ++;
					tt1 = t3,tt2 = t1,tt3 = t2;
					tt2 ++;  //noice
				}
				else{
				//	t3 ++;
					tt1 = t2,tt2 = t3,tt3 = t1;
					tt3 ++;  //noice
				}
				ans += tt1;
				t1 = tt1,t2 = tt2,t3 = tt3;
			}
			else{
				t1 = t2 = t3 = 0;
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}
/*4 3
BWWWWWBWBWBWWBWBWBWWWB
6 2
BWWBWBWWWBWBBBBW
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值