File size: 1,002 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * Written by Nitin Kumar Maharana
 * [email protected]
 */

#include <iostream>
#include <string>
#include <map>

using namespace std;

string input;
map<int, unsigned long long> memory;
map<int, unsigned long long>::iterator it;
int len;

unsigned long long findNumDecoding(int index)
{
	if(index == len || index == len-1)
		return 1ull;

	it = memory.find(index);

	if(it != memory.end())
		return (it->second);

	unsigned long long result;

	if(input[index] > '2' || (input[index] == '2' && input[index+1] > '6') || (index+2 < len && input[index+2] == '0'))
		result = findNumDecoding(index+1);
	else if(input[index+1] == '0')
		result = findNumDecoding(index+2);
	else
		result = (findNumDecoding(index+1) + findNumDecoding(index+2));

	memory.insert(make_pair(index, result));

	return result;
}

int main(void)
{
	cin >> input;

	while(input != "0")
	{
		len = input.length();

		cout << findNumDecoding(0) << endl;

		input.clear();
		memory.clear();

		cin >> input;
	}

	return 0;
}