forked from jaideeppyne/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphacode(SPOJ).cpp
More file actions
34 lines (31 loc) · 1002 Bytes
/
Alphacode(SPOJ).cpp
File metadata and controls
34 lines (31 loc) · 1002 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
//http://www.spoj.com/problems/ACODE/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
std::ios::sync_with_stdio(0);cin.tie(0);
while(true)
{
string s;cin >> s;
if(s[0] == '0') return 0;
int n = s.length();
s = ' ' + s;
vector<ll>dp(n + 1);
fill(dp.begin() , dp.end() , 0);//populate with zeroes
dp[0] = dp[1] = 1;
//dp[1] is needed for 1 length strings
//dp[0] is needed for 2 length strings
for(int i = 2;i <= n;i++)
{
if(s[i] != '0') dp[i] = dp[i - 1];
//current valid character append
int last_2_char = (s[i - 1] - '0') * 10 + (s[i] - '0');
if(last_2_char <= 26 && last_2_char >= 10)
dp[i] += dp[i - 2]; // last 2 valid characters append
}
cout << dp[n] << "\n";
}
}
//tags - dynamic programming(basic)
//Overall Complexity - O(|s|)