Write an LC-3 program to test whether a string contains the sequence \"aaa\". As
ID: 3776102 • Letter: W
Question
Write an LC-3 program to test whether a string contains the sequence "aaa". Assume that the string starts at the memory location 0 times 4000, and is terminated by a '' character (ASCII value = 0). If the string contains the sequence, your program should store 1 at the memory location 0 times 5000, otherwise store 0 at memory location 0 times 5000. You need to do the following things in this program: Continuously read a sequence of characters starting at 0 times 4000. Check for the termination character *=''. Come up with an idea to detect the sequence "aaa". When the sequence "aaa'' is detected, store 1 at 0 times 5000 and halt the program. If the end of the string is met and "aaa" has not been detected, store 0 at 0 times 5000. A few sample runs of the program are as follows: Input: "abcdaa" 0 times 5000:0 Input: "aaafg()" 0 times 5000:1 Input: "tyeaayuaaaa" 0 times 5000: 1Explanation / Answer
this is pretty easy if you think in terms of Dp approach.
Lets say we have String as "1" so our output will be 1 because we only form 'A'
but if we add 2 to the existing string "12" then output is "AB" and "L" so if you see
the logic is when you add a character to an existing string the number of combinations increase by combining last 2 digits
in dp it can be formulized As
d[i]=dp[i-1]+dp[i-2](if the last 2 digits combined is smaller than 27)
else dp[i]=dp[i-1];
code is as below
}
Program 2:
public int subStringReprensation(String inputNumber){
char[] inputChars=inputNumber.toCharArray();
int count=0;
if(inputChars.length==1){
return 1;
}
int i=0;
StringBuffer numberSampleString =new StringBuffer();
int numberSample=0;
while(i < inputChars.length){
numberSampleString.append(inputChars[i]);
numberSample=new Integer(numberSampleString.toString());
i++;
if(numberSample>26) break;
if(numberSampleString.length()==inputNumber.length()&& numberSampleString.length()==2 && numberSample<=26){
count++;
}else{
count+=subStringReprensation(inputNumber.substring(i,inputChars.length));
}
}
return count;
}
Dynamic programming version of my previous answer:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.