Title: Http://acm.hnu.cn/online/?action=problem&type=show&id=13303&courseid=0
Test instructions: give you a string, consisting of numbers and other characters, asking how many substrings, so that the number inside the substring and the integer multiples of 3 (the substring must be continuous, and there can be no other characters).
Analysis: Open 3 arrays, dp0[i],dp1[i],dp2[i]. Dp0[i] Represents the number of strings from the last position of the string to the position I have and is a multiple of 0, Dp1[i],dp2[i] is similar. Push forward from the back, so if the current position is I, (str[i]-' 0 ')%3==1,dp0[i]=dp2[i+1],。。。。
Code:
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 1e6+10; Char Str[maxn];long long dp0[maxn],dp1[maxn],dp2[maxn];int main () {int len,i,j,x;long long ans,sum;while (scanf ("%s", STR)!=eof {len=strlen (str); memset (Dp0,0,sizeof (dp0[0)) * (len+2)); Memset (Dp1,0,sizeof (dp0[0)) * (len+2)); Memset ( Dp2,0,sizeof (Dp0[0]) * (len+2)) ans=0;for (i=len-1;i>=0;i--) {if (str[i]>= ' 0 ' && str[i]<= ' 9 ') {x= (str [i]-' 0 ')%3;if (x==1) {ans+=dp2[i+1];dp 0[i]=dp2[i+1];DP 1[i]=dp0[i+1];DP 2[i]=dp1[i+1];DP 1[i]++;} else if (x==2) {ans+=dp1[i+1];dp 0[i]=dp1[i+1];DP 1[i]=dp2[i+1];DP 2[i]=dp0[i+1];DP 2[i]++;} ELSE{ANS+=DP0[I+1]+1;DP0[I]=DP0[I+1];DP 1[i]=dp1[i+1];DP 2[i]=dp2[i+1];DP 0[i]++;}} else{dp0[i]=dp1[i]=dp2[i]=0;}} cout<<ans<< ' \ n ';} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HNU13303 counting substhreengs (Recursive)