Counting substhreengs(UVALive 6823)

Description

Substrings are strings formed by choosing a subset of contiguous characters from a string. This is well
known. A little more obscure is the definition of substhreengs. A substhreeng is a substring which
complies to the following additional requirements:

  1. It is non-empty, and composed entirely of base 10 digits.
  2. Interpreted in base 10 (allowing extra leading zeros), the resulting integer is a multiple of 3.
    For instance, the string “130a303” contains 9 substhreengs: the substhreeng ‘3’ three times, the
    substhreengs ‘30’ and ‘0’ twice each, and the substhreengs ‘303’ and ‘03’ once each. The substring
    ‘30a3’ is not a substhreeng because it is not composed entirely of base 10 digits, while the substring
    ‘13’ is not a substhreeng because 13 is not a multiple of 3.
    Notice that two substhreengs are considered different if they are different in length or start at a
    different position, even if the selected characters are the same.
    Given a string, you are asked to count the number of substhreengs it contains.
Input

The input contains several test cases; each test case is formatted as follows. A test case consists of a
single line that contains a non-empty string S of at most 106
characters. Each character of S is either
a digit or a lowercase letter.

Output

For each test case in the input, output a line with an integer representing the number of substhreengs
contained in S.

Sample Input

130a303
0000000000
icpc2014regional

Sample Output

9
55
2

题解:

查找所给串中有多少子串可以被3整除,中间不能夹杂字母。dp数组记录当前选定情况,dp[0]记录余数为0的个数…设当前位置为子串的最后一位,如果%3等于1,那么前面dp[2]的数加上当前数能变成余数0的数,所以dp[0]=dp[2],dp[1]=dp[0]+1(余数为0的加上当前值),dp[2]=dp[1] (前dp[1]个余数为1的加上当前数余数变为2).如果%3等于2,那么前面dp[1]的数加上当前数能变成余数0的数,所以dp[0]=dp[1],dp[1]=dp[2] (前dp[2]个余数为2的加上当前数余数变为1),dp[2]=dp[0]+1(余数为0的加上当前值).注意:(要开long long )(=前后dp不是同一阶段的).

代码如下:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 27
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1001113
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;


char a[1000010];
ll dp[3];
int main()
{
    while(cin>>a)
    {
        int l=strlen(a);
        ll ans=0;
        for(int i=0;i<l;i++)
        {
            if(a[i]>='0'&&a[i]<='9')
            {
                int k=(a[i]-'0')%3;
                int d0,d1,d2;
                if(k==0)
                {
                    d0=dp[0]+1;
                    d1=dp[1];
                    d2=dp[2];
                }
                if(k==1)
                {
                    d0=dp[2];
                    d1=dp[0]+1;
                    d2=dp[1];
                }
                if(k==2)
                {
                    d0=dp[1];
                    d1=dp[2];
                    d2=dp[0]+1;
                }
                dp[0]=d0;
                dp[1]=d1;
                dp[2]=d2;
                ans+=dp[0];
                //cout<<dp[0]<<" "<<dp[1]<<" "<<dp[2]<<endl;
            }
            else
            {
                dp[0]=dp[1]=dp[2]=0;
            }
        }
        cout<<ans<<endl;
        memset(a,0);
        memset(dp,0);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值