Super Six Substrings

  • + 0 comments

    include

    include

    char s[100005]; int arr[100005][3];

    int fun(int x,int m){

    if(x>=strlen(s))
    return 0;
    else if(arr[x][m]!=-1)
    return arr[x][m];
    else {
         if((s[x]-'0'+m)%3==0 && (s[x]-'0')%2==0){
        arr[x][m]=fun(x+1,(s[x]-'0'+m)%3)+1;
    }
        else {
        arr[x][m]=fun(x+1,(s[x]-'0'+m)%3); }
        return arr[x][m];
    }
    

    }

    int main(){ scanf(" %s",s); int j,i=0; long long ans=0; for(j=0;j<100005;j++){ arr[j][0]=arr[j][1]=arr[j][2]=-1;} while(s[i]!='\0'){ if((int)s[i]=='0'){

      ans=ans+1;
      }
    else {
        ans=ans+fun(i,0);
    
        }
    
            i++;}
        printf("%lld",ans);
    
    return 0;}
    //can anyone please help out.
    //my above code is giving time out for test cases above 12