-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dp_n.cpp
44 lines (37 loc) · 954 Bytes
/
AC_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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dp_n.cpp
* Create Date: 2015-02-26 12:07:51
* Descripton: dp[i] = dp[i - 1](if s[i] is valid) + dp[i - 2](if s[i,i+1] is valid)
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int numDecodings(string s) {
if (s.length() == 0)
return 0;
if (s.length() == 1)
return s[0] != '0';
int dp[2], rec;
dp[0] = 1;
dp[1] = s[0] != '0';
for (int i = 2; i <= s.length(); ++i) {
rec = 0;
if (s[i - 1] != '0')
rec += dp[(i - 1)&1];
if (s.substr(i - 2, 2) <= "26" && s[i - 2] != '0')
rec += dp[i&1];
dp[i&1] = rec;
}
return dp[s.length()&1];
}
};
int main() {
string ss;
Solution s;
while (cin >> ss)
cout << s.numDecodings(ss) << endl;
return 0;
}