Skip to content

Instantly share code, notes, and snippets.

@cypher-nullbyte
Last active May 21, 2020 19:56
Show Gist options
  • Save cypher-nullbyte/c7950a3b5dda6b07e6037fcdb09fd74c to your computer and use it in GitHub Desktop.
Save cypher-nullbyte/c7950a3b5dda6b07e6037fcdb09fd74c to your computer and use it in GitHub Desktop.
Vpropel VIT | POD | 18/05/2020 | Cryptography-II | 08
//Dp
#include <iostream>
#include<string>
using namespace std;
unsigned long int MOD=1000000007;
unsigned long int counter(string digits, unsigned long int n)
{
unsigned long int count[n+1]={0};
count[0] = 1;
if(digits[0]=='*')
count[1]=9;
else
count[1] = 1;
for (int i = 2; i <= n; i++)
{
if(digits[i-2]=='*' && digits[i-1]!='*')
{
count[i]=count[i-1];
char varnum='1';
while(varnum<='9')
{
if((varnum<='2' && digits[i-1]<='6')||varnum=='1')
count[i]=(count[i]+count[i-2])%MOD;
varnum++;
}
}
else if(digits[i-1]=='*' && digits[i-2]!='*')
{
count[i]=(9*count[i-1])%MOD;
char varnum='1';
while(varnum<='9')
{
if((varnum<='6' && digits[i-2]<='2')||digits[i-2]=='1')
count[i]=(count[i]+count[i-2])%MOD;
varnum++;
}
}
else if(digits[i-1]=='*' && digits[i-2]=='*')
{
count[i]=(9*count[i-1])%MOD;
char v1='1';
char v2;
for(v1;v1<='9';v1++)
for(v2='1';v2<='9';v2++)
{
if((v1<='2'&& v2<='6')||v1=='1')
count[i]=(count[i]+count[i-2])%MOD;;
}
}
else
{
count[i] = count[i-1];
if((digits[i-2] <='2' && digits[i-1] <='6')||digits[i-2]=='1')
count[i]=(count[i]+count[i-2])%MOD;;
}
}
/*
// To see how Table is formed and DP applied :)
for(int i=0;i<=n;i++)
cout<<digits[i]<<"\t";
cout<<endl;
for(int i=0;i<=n;i++)
cout<<count[i]<<'\t';
*/
return (unsigned long int)count[n];
}
int main()
{
string digits;
cin>>digits;
unsigned long int n = digits.length();
cout<< counter(digits,n);
return 0;
}
// ### Thank YOu :)
// @cs_jawanda
// .Chiranjeet Singh Jawanda.
// VIT VELLORE
//Dp
import java.util.Scanner;
public class Main
{
public static void counter(char []digits,int n)
{
final long MOD=1000000007;
long count[]=new long[n+1];
count[0] = 1;
if(digits[0]=='*')
count[1]=9;
else
count[1] = 1;
for (int i = 2; i <= n; i++)
{
if(digits[i-2]=='*' && digits[i-1]!='*')
{
count[i]=count[i-1];
count[i]=(count[i]+count[i-2]*(digits[i-1]<='6'?2:1))%MOD;
}
else if(digits[i-1]=='*' && digits[i-2]!='*')
{
count[i]=(9*count[i-1])%MOD;
char varnum='1';
while(varnum<='9')
{
if((varnum<='6' && digits[i-2]<='2')||digits[i-2]=='1')
count[i]=(count[i]+count[i-2])%MOD;
varnum++;
}
}
else if(digits[i-1]=='*' && digits[i-2]=='*')
{
count[i]=(9*count[i-1])%MOD;
count[i]=(count[i]+count[i-2]*15)%MOD;
}
else
{
count[i] = count[i-1];
if((digits[i-2] <='2' && digits[i-1] <='6')||digits[i-2]=='1')
count[i]=(count[i]+count[i-2])%MOD;;
}
}
System.out.print(count[n]);
}
public static void main(String []args)
{
Scanner s=new Scanner(System.in);
String str=s.next();
char digits[]=str.toCharArray();
counter(digits,digits.length);
}
}
// ### Thank YOu :)
// @cs_jawanda
// .Chiranjeet Singh Jawanda.
// VIT VELLORE
//Recursion using Memoisation
import java.util.Scanner;
class Solve
{
long M=1000000007;
public int numDecodings(String s)
{
Integer[]memo=new Integer[s.length()];
return ways(s,s.length()-1,memo);
}
public int ways(String s,int i,Integer[] memo)
{
if(i<0) return 1;
if(memo[i]!=null) return memo[i];
if(s.charAt(i)=='*')
{
long res=9*ways(s,i-1,memo);
if(i>0 && s.charAt(i-1)=='1')
res=(res+9*ways(s,i-2,memo))%M;
else if(i>0 && s.charAt(i-1)=='2')
res=(res+6*ways(s,i-2,memo))%M;
else if(i>0 && s.charAt(i-1)=='*')
res=(res+15*ways(s,i-2,memo))%M;
memo[i]=(int)res;
return memo[i];
}
long res=s.charAt(i)!='0'?ways(s,i-1,memo):0;
if(i>0 && (s.charAt(i-1)=='1' ||(s.charAt(i-1)=='2' && s.charAt(i)<='6')))
res=(res + ways(s,i-2,memo))%M;
else if(i>0 && s.charAt(i-1)=='*')
res=(res+(s.charAt(i)<='6'?2:1)*ways(s,i-2,memo))%M;
memo[i]=(int)res;
return memo[i];
}
}
public class Practice
{
public static void main(String []args) throws Exception
{
Scanner s=new Scanner(System.in);
String str=s.next(); s.close();
Solve obj=new Solve();
System.out.println(obj.numDecodings(str));
}
}
Cryptography-II
A message containing letters from A-Z is being encoded to numbers using the following mapping way:'A' -> 1,'B' -> 2,'Z' -> 26
Beyond that, now the encoded string can also contain the character '*', which can be treated as one of the numbers from 1 to 9.
Given the encoded message containing digits and the character '*', return the total number of ways to decode it.
For example, 1* has got 18 decoding possibilities as shown below
(i) 1* - 1 may be decoded as A and * can take values from 1 to 9
(ii) 1* - Keep 1 as 1 and * can take values from 1 to 9 then characters corressponding to 11 to 19 will be deocoded
Input Format
First line contains the encoded string
Output Format
Print the decoded string
Also, since the answer may be very large, you should return the output mod 10^9 + 7.
@cypher-nullbyte
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment