HNU13303 counting substhreengs (Recursive)

Source: Internet
Author: User

Title: Http://

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],。。。。


#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.