-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacode.cpp
51 lines (46 loc) · 1.38 KB
/
acode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<cstdio>
#include<cstring>
using namespace std;
long long compute(long long* dpArray, const char* array, int len);
long long compute(long long* dpArray, const char* array, int len, int index);
int main() {
char array[5001];
while(true) {
scanf("%s", array);
int len = strlen(array);
if(strcmp(array, "0") == 0) {
return 0;
}
long long* dpArray = new long long[len + 1];
for(int i=0;i<len + 1;++i){
dpArray[i] = -1;
}
dpArray[0] = 1;
long long val = compute(dpArray, array, len);
printf("%lld\n", val);
}
}
long long compute(long long* dpArray, const char* array, int len) {
return compute(dpArray, array, len, len);
}
long long compute(long long* dpArray, const char* array, int len, int index) {
if(dpArray[index] != -1) {
return dpArray[index];
}
char val = array[index - 1];
long long count = 0;
// single character case
if(val > '0' && val <= '9') {
count = compute(dpArray, array, len, index - 1);
}
int num = 0;
if(index > 1 && array[index-2] != '0') {
num += (array[index - 1] - '0');
num += 10 * (array[index - 2] - '0');
if(num > 0 && num <= 26) {
count += compute(dpArray, array, len, index - 2);
}
}
dpArray[index] = count;
return dpArray[index];
}