nowcoderD Xieldy And His Password

题意:一个01序列,长度1e6,问有多少子串十进制是3的倍数

题解:DP[i][j]代表前i个并且以i为结尾,且十进制%3==j的串的个数

#include<bits/stdc++.h>
#define maxn 1001000
using namespace std;
char s[maxn];
long long dp[maxn][3], sum;
int main(){
    while(~scanf("%s", s+1)){
        sum = 0;
        int l = strlen(s+1);
        for(int i=1;i<=l;i++){
            if(s[i] == '0'){
                dp[i][0] = dp[i-1][0]+1;
                dp[i][2] = dp[i-1][1];
                dp[i][1] = dp[i-1][2];
            }
            else{
                dp[i][0] = dp[i-1][1];
                dp[i][1] = dp[i-1][0]+1;
                dp[i][2] = dp[i-1][2];
            }
            sum += dp[i][0];
        }
        cout<<sum<<endl;
    }

    return 0;
}

 

posted on 2018-04-21 18:04  2855669158  阅读(140)  评论(0编辑  收藏  举报

导航