requirements
write c++ code for the above programs use only 1D arrays with loops
and if else statement don't use any other way as it would be
useful
Answer all four parts with comments for better understanding
There are only two questions in Pictures provided above out of which one question i.e spy and decoding is repeated three times. So effectively only two questions:
Question 1. Spy and Decoding
This question has been solved using dynamic programming most of the logic has been discussed in the code with the help of comments I hope that you find it useful.
CODE :
#include <bits/stdc++.h> //including the necessary
libraries
using namespace std;
//Decoding Function which return number of possible
decodings
int decoding(string sequence){
int n = sequence.size(); // calculating the size
of the digit sequence
int dp[n+1]; //dp table in which dp[i] denotes
the number of possible decodings of the sequence upto length
i
dp[0] = 1; //base case
dp[1] = 1; //base case
if(sequence[0] == '0') return 0; //Invalid case
as if any sequence starts with 0 then it is invalid Eg: 01122
for(int i = 2;i <= n; i++){
//initializing the dp
value to be 0
dp[i] = 0;
//if the previous
digit was greater than 0 then it would also contribute upto next
length also
if(sequence[i-1] >
'0') dp[i] = dp[i-1];
//Here we handle the
two digit case as we lookup behind upto two indexes in the
sequence
//If the digit 2 places
behind is 1 then we can have any digit at the next position
as
//all 2 digits numbers
having 1 at it's ten's place would be valid Eg: 10,11,12,13 ...
19
//But if the digit 2
places behind is 2 then we only have 7 choices namely
20,21,22,23,24,25,26
// Hence we check only
upto 6 by using the < 7 condition.
if(sequence[i-2] == '2'
&& sequence[i-1] < '7' || sequence[i-2] == '1') dp[i] +=
dp[i-2];
}
return dp[n]; //returning the answer for the
length n
}
//DRIVER CODE//
int main(){
string digit_sequence1 = "123"; //Valid with
answer 3
string digit_sequence2 = "204"; //Valid with
answer 1
string digit_sequence3 = "603"; //InValid with
answer 0
cout<<"The Number of Possible Decodings
for digit_sequence1 are :
"<<decoding(digit_sequence1)<<endl;
cout<<"The Number of Possible Decodings
for digit_sequence2 are :
"<<decoding(digit_sequence2)<<endl;
cout<<"The Number of Possible Decodings
for digit_sequence3 are :
"<<decoding(digit_sequence3)<<endl;
}
---------------------------------------
Screenshots:
Decoding Function :
Driver Code :
Compiling & Running:
Output :
/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/
Question 2: Pattern Finding
Code:
#include <bits/stdc++.h> //including the necessary
libraries
using namespace std;
int main(){
string sentence; //declaraing sentence
variable
cin>>sentence; //taking input
for the sentence
string pattern; //declaraing pattern
variable
cin>>pattern; //taking
input for the pattern
int s_length = sentence.length();
//calculating length of sentence
int pat_length = pattern.length();//calculating
length of pattern
//running the loop on sentence from index 0
to (sentence length - pattern length)
//as it is only feasible to run the loop upto
(sentence length - pattern length)
//because after this any substring would be of
less length then pattern length
//Eg: sentence : elephant pattern : ant ----
here we will run the loop only till
//a of ant in elephant as after this all sub
strings will have length shorter than
//length of pattern i.e ant here
for(int
i=0;i<s_length-pat_length+1;i++){
bool flag = true;
//using a flag varible to check if the pattern has been found
int j=0; //iterator
variable
//searching for the
pattern in the sentence
for(j=0;j<pat_length;j++){
if(sentence[i+j] == pattern[j]) continue;
else{
flag = false; //setting the flag varible false if pattern doesn't
matches at any place
break; //breaking the loop
}
}
//NOTE : Here we are
printing the index based on 1-based indexing
//Eg : ELEPHANT ==
12345678
//checking if the
pattern has been found or not
if(flag)
cout<<i+1<<"--"<<i+j<<endl; //printing the
index of pattern in the sentence
}
}
Screenshots:
Code:
Compiling :
Running and Output :
Happy Coding !
requirements write c++ code for the above programs use only 1D arrays with loops and if...
Do this using the C language. show me the code being executed and also copy and paste the code so i can try it out for myseld Instructions A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (ie. "scrambled"). Ciphers were an early form of security used to send hidden messages from one party to another. The most famous and classic example of a cipher is the Caesar Cipher. This cypher worked by shifting each letter...
Is the 2-cycle (1 2) in the subgroup H= 9 generated by and ? Explain answer. , T> 0 We were unable to transcribe this imageWe were unable to transcribe this image(123)(45)(6789) (13)(578)(49)
Let X1, X2, ..., Xn be a random sample from X which has pdf depending on a parameter and (i) (ii) where < x < . In both these two cases a) write down the log-likelihood function and find a 1-dimensional sufficient statistic for b) find the score function and the maximum likelihood estimator of c) find the observed information and evaluate the Fisher information at = 1. f(20) We were unable to transcribe this image((z(0 – 2) - )dxəz(47)...
Can you check my work? This is a Gauss Law question. We have to find the electric field z away from the sheet and z > 0 and z < 0. My Work: Is this correct? Thanks. The Question: EdA Qenclosed Eo We were unable to transcribe this imageenclosedTy We were unable to transcribe this imageWe were unable to transcribe this imageWe were unable to transcribe this imageAn infinite sheet of charge on the x-y plane carries a uniform surface...
The behavior of a spin- particle in a uniform magnetic field in the z-direction, , with the Hamiltonian You found that the expectation value of the spin vector undergoes Larmor precession about the z axis. In this sense, we can view it as an analogue to a rotating coin, choosing the eigenstate with eigenvalue to represent heads and the eigenstate with eigenvalue to represent tails. Under time-evolution in the magnetic field, these eigenstates will “rotate” between each other. (a) Suppose...
Let be the distribution function defined by Let be the Lebesgue-Stieltjes measure asociated to . Determine the measurements of the fpllowing sets: We were unable to transcribe this imageF(x) = 0 if 1+r if 2+x? if 19 if I <-1 -1 <r <0 0<r<2 > 2 We were unable to transcribe this imageWe were unable to transcribe this imageWe were unable to transcribe this imageWe were unable to transcribe this image(-1, 0]υ (1,2) 10, ) U (1, 2 (1 :...
Negative binomial probability function: is the mean is the dispersion parameter Let there be two groups with numbers and means of 1) Write down the log-likelihood for the full model 2) Calculate the likelihood equations and find the general form of the MLE for and 3) Write down the likelihood function in the reduced model (ie. assuming ) and derive the MLE for in general terms 4) Using the above answers only, give the MLE and standard error for where...
Let X1, X2, ..., Xn be a random sample of size n from the distribution with probability density function To answer this question, enter you answer as a formula. In addition to the usual guidelines, two more instructions for this problem only : write as single variable p and as m. and these can be used as inputs of functions as usual variables e.g log(p), m^2, exp(m) etc. Remember p represents the product of s only, but will not work...
1) A particle with mass m moves under the influence of a potential field . The particle wave function is stated by: for where and are constants. (a) Show that is not time dependent. (b) Determine as the normalization constant. (c) Calculate the energy and momentum of the particle. (d) Show that V (x /km/2h+it/k/m Aar exp (ar, t) We were unable to transcribe this imageWe were unable to transcribe this imageWe were unable to transcribe this imageWe were unable...
A Pareto distribution is often used in economics to explain a distribution of wealth. Let a random variable X have a Pareto distribution with parameter θ so that its probability distribution function is for and 0 otherwise. The parameters and are known and fixed; is a constant to be determined. a) Assuming that find the expected value and variance of ? b) Show that for 3 ≥ θ > 2 the Pareto distribution has a finite mean but infinite variance,...