-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_recursive_dp_n.cpp
46 lines (40 loc) · 1.05 KB
/
AC_recursive_dp_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_recursive_dp_n.cpp
* Create Date: 2015-02-26 11:34:39
* Descripton: dp, recursive and memorial search, O(n) space and O(n) time
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
// record the ans of (string begin in 'pos')
int dp(int pos, string &s, vector<int> &rec) {
if (rec[pos] != -1)
return rec[pos];
int ret = 0;
// one number
if (s[pos] > '0')
ret += dp(pos + 1, s, rec);
// two number
if (pos + 1 < s.length() && s.substr(pos, 2) >= "10" && s.substr(pos, 2) <= "26")
ret += dp(pos + 2, s, rec);
return rec[pos] = ret;
}
public:
int numDecodings(string s) {
if (s == "")
return 0;
vector<int> rec(s.length() + 1, -1);
rec[s.length()] = 1;
return dp(0, s, rec);
}
};
int main() {
string ss;
Solution s;
while (cin >> ss)
cout << s.numDecodings(ss) << endl;
return 0;
}