Skip to content

Commit 089abdd

Browse files
authored
Merge pull request ashutosh97#119 from Udit-8/master
Added question alphacode
2 parents 2d1230a + 27c0bcd commit 089abdd

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

alphacode/problem_statement.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages:
2+
3+
Alice: “Let’s just use a very simple code: We’ll assign ‘A’ the code word 1, ‘B’ will be 2, and so on down to ‘Z’ being assigned 26.”
4+
5+
Bob: “That’s a stupid code, Alice. Suppose I send you the word ‘BEAN’ encoded as 25114. You could decode that in many different ways!”
6+
Alice: “Sure you could, but what words would you get? Other than ‘BEAN’, you’d get ‘BEAAD’, ‘YAAD’, ‘YAN’, ‘YKD’ and ‘BEKD’. I think you would be able to figure out the correct decoding. And why would you send me the word ‘BEAN’ anyway?”
7+
Bob: “OK, maybe that’s a bad example, but I bet you that if you got a string of length 5000 there would be tons of different decodings and with that many you would find at least two different ones that would make sense.”
8+
Alice: “How many different decodings?”
9+
Bob: “Jillions!”
10+
11+
For some reason, Alice is still unconvinced by Bob’s argument, so she requires a program that will determine how many decodings there can be for a given string using her code.
12+
13+
INPUT
14+
15+
Input will consist of multiple input sets. Each set will consist of a single line of at most 5000 digits representing a valid encryption (for example, no line will begin with a 0). There will be no spaces between the digits. An input line of ‘0’ will terminate the input and should not be processed.
16+
17+
OUTPUT
18+
19+
For each input set, output the number of possible decodings for the input string. All answers will be within the range of a 64 bit signed integer.
20+
21+
Example
22+
23+
Input:
24+
25+
25114
26+
1111111111
27+
3333333333
28+
0
29+
30+
Output:
31+
32+
6
33+
89
34+
1

alphacode/solution.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
using namespace std;
5+
6+
int main(){
7+
int L;
8+
char d[5001];
9+
long long dp[5001];
10+
11+
while(true){
12+
scanf("%s",d);
13+
if(d[0]=='0') break;
14+
15+
L = strlen(d);
16+
17+
dp[0] = dp[1] = 1;
18+
19+
for(int i = 2;i<=L;++i){
20+
dp[i] = 0;
21+
22+
char c1 = d[i-2]-'0', c2 = d[i-1]-'0';
23+
24+
if(c1==1 || (c1==2 && c2<=6)) dp[i] += dp[i-2];
25+
if(c2!=0) dp[i] += dp[i-1];
26+
}
27+
28+
printf("%lld\n",dp[L]);
29+
}
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)