Hi,
Qsn:- http://www.spoj.com/problems/ACODE/
Ans link:- http://ideone.com/vnHU3u
Any test case you think is missing.I have taken 08 as invalid input as per in comments as unclear in question,but in took kit 08 is taken as valid.
include
using namespace std;
int length(string a) {
int i=0;
while(a[i])
i++;
return i;
}
int numberPossibilites(string input, int dp[],int index){
int ans = 0, ansNext=0;
if(input[index] == '\0') {
dp[index]=1;
return 1;
} else {
if(!dp[index]){
if(input[index+1] != '\0'){
if(input[index] > '0' && input[index] < '3' && input[index+1] < '7'){
if(!dp[index+2]){
ans = numberPossibilites(input, dp, index+2);
} else {
ans = dp[index+2];
}
}
}
if(input[index] != '0'){
if(!dp[index+1]){
ansNext = numberPossibilites(input, dp, index+1);
ans += ansNext;
} else {
ans += dp[index+1];
}
}
dp[index] = ans;
}
}
return dp[index];
}
int main() {
string input;
while(true){
cin >> input;
if(input[0] == '0' && input[1] == '\0')
break;
int len = length(input);
int *dp = new int[len+1]();
cout << numberPossibilites(input, dp, 0) << "\n";
}
}