#include #include #include #include #include using namespace std; class DigitStringDiv2 { public: int count(string S, long long X); }; int DigitStringDiv2::count(string S, long long X) { int i, j; int c; char *s; int total; long long val; /* Make a C-string copy of the string */ s = strdup(S.c_str()); total = 0; /* Enumerate all starting and ending indices: */ for (i = 0; i < S.size(); i++) { if (s[i] != '0') { for (j = i+1; j <= S.size(); j++) { /* Copy the end character, set it to the null character, convert with sscanf and restore the end character. */ c = s[j]; s[j] = '\0'; sscanf(s+i, "%lld", &val); if (val > X) total++; s[j] = c; } } } /* Return the total. Since you want to avoid memory leaks, you should free s too. */ free(s); return total; }