-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalphacode.java
75 lines (67 loc) · 1.45 KB
/
alphacode.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.*;
class alphacode{
public static int ans;
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
int x=Integer.parseInt(Character.toString(s.charAt(0)));
while(x!=0){
ans=0;
int a[]=new int[s.length()+1];
for(int i=1;i<=s.length();i++){
a[i]=Integer.parseInt(Character.toString(s.charAt(i-1)));
}
// function(a,1,a.length);
long dp[]=new long[a.length+1];
dp[0]=1;
dp[1]=1;
int n=s.length()+1;
for(int i=2;i<n;i++){
if(a[i]==0){
dp[i]=dp[i-2];
}else if(a[i]<=6){
if(a[i-1]==1 || a[i-1]==2){
dp[i]=dp[i-1]+dp[i-2];
}else{
dp[i]=dp[i-1];
}
}else{
if(a[i-1]==1 || a[i-1]==2){
dp[i]=dp[i-1]+dp[i-2];
}else{
dp[i]=dp[i-1];
}
}
// System.out.println(a[i]+" "+dp[i]);
}
// ans=dp[a.length-1];
System.out.println(dp[a.length-1]);
s=br.readLine();
x=Integer.parseInt(Character.toString(s.charAt(0)));
}
}
public static void function(int a[],int i,int n){
if(i>=n){
ans+=1;
return;
}else{
if(a[i]==1){
if(i<n-1){
if(a[i+1]==0){
function(a,i+1,n);
}else{
function(a,i+1,n);
function(a,i+2,n);
}
}else{
function(a,i+1,n);
}
}else if(a[i]==2){
function(a,i+1,n);
function(a,i+2,n);
}else{
function(a,i+1,n);
}
}
}
}