-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalphaCode.cpp
More file actions
66 lines (49 loc) · 993 Bytes
/
alphaCode.cpp
File metadata and controls
66 lines (49 loc) · 993 Bytes
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
long m = 10e9+7;
long getNoOfDecodings(int *n, int size, int *dp)
{
if(size == 0 || size == 1)
{
return 1;
}
if(dp[size] != -1)
{
return dp[size];
}
long output = 0;
if(n[size-1] != 0)
{
output = getNoOfDecodings(n, size-1, dp);
}
if(n[size-2] * 10 + n[size-1] <= 26 && n[size-2] != 0)
{
output = (output + getNoOfDecodings(n, size-2, dp)) % m;
}
dp[size] = output;
return output;
}
int main()
{
string code;
cin >> code;
do
{
int *n = new int[code.length()];
for(int i = 0; i < code.length(); i++)
{
n[i] = code[i] - '0';
}
int *dp = new int[code.length()+1];
for(int i = 0; i <= code.length(); i++)
{
dp[i] = -1;
}
long ans = getNoOfDecodings(n, code.length(), dp);
cout << ans << endl;
delete [] n;
delete [] dp;
cin >> code;
}while(code[0] != '0');
return 0;
}