Question

Answer the following questions about the algorithm below, which searches an input string for the substring ab and returns where ab first appears, or -1 if ab is not in the string. Note that ] is the floor operator, which rounds its input down to the nearest integer Input: str: a string of length n Input: n the length of str Output: the first index i such that stria and strib, or -1 if str doesnt contain the substring ab 1 Algorithm: abSearch 2 if n< 2 then 3return -1 4 else if n 2 then if str= ab then 6 return 1 7 else return-1 9end 10 else | midpt=ln/2] 11 12left-abSearch(str[1..midpt]) 13 | center = abSearch(str[midpt..midpt + 11) 14right- abSearch(str[midpt 1..n]) 15if left t-1 then 16 17else if center -1 then 18 19else if right -1 then 20 21 return left return midpt return right +midpt return-1 else 23 24 end

2. Prove that abSearch returns ?1 if str does not contain “ab” as a substring. You may omit the base case for this proof (done in problem 1). Hint: you will need to think carefully about the different cases of the if statement in lines 15–23 to show that abSearch always returns ?1 when str doesn’t contain “ab.”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution Weat ohcetunst doesnot Con tuh ab as a sustin b bba #cte neu (lengtんof-the stin! ) Na) let us apply Hufs algonithm t

ineut be executa d elx to enlo ไก่ห be enreutad I teun but we got fe So eex uill be eeutad Cente So elx be enuculad rng ut go

-1 tiene is obtained stlu u Help

Add a comment
Know the answer?
Add Answer to:
2. Prove that abSearch returns ?1 if str does not contain “ab” as a substring. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • This is slice operator : [i : j] (Function) Slice : Returns the substring from i...

    This is slice operator : [i : j] (Function) Slice : Returns the substring from i to j -1 Quetion: 1) using the slice operator, print your first, then last name . 2) Print the length of your first name. 3) Assume you have two variables : s=‘s’ and p=‘p’ . Using concatenation and repetition, write an expression that produced the string mississippi. please attach your code and output

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

  • Hello, I'm having trouble with this certain part to a project of mine. Say I have...

    Hello, I'm having trouble with this certain part to a project of mine. Say I have two strings, String left and String right. This is part of an expression evaluation problem, and I need to have a certain piece of the expression in left and a certain piece of the expession in right. The code I have works with everything except if * or / come before + or -. No parentheses included, I have another method that deals with...

  • Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c...

    Please read the problem carefully and answer the 2 questions below code: /***************************************************************** * Program: palindrome.c * * Purpose: implements a recursive function for determining *   if a string is a palindrome * * Authors: Steven R. Vegdahl, Tammy VanDeGrift, Martin Cenek * *****************************************************************/ #include #include #include /***************************************************************** * is_palindrome - determines whether a string of characters is a palindrome * * calling sequence: *    result = is_palindrome(str, first_index, last_index) * * parameters - *    str - the string to test *    first_index -...

  • 1. What is the height of a full binary search tree that contains 63 nodes 2....

    1. What is the height of a full binary search tree that contains 63 nodes 2. What is the output of this code? Describe what this recursive code does. public class Driver {    public static void main (String[ ] args)   {        String text = “what do I do”;        System.out.println(Mystery.task(text));   } } public class Mystery {    public static int task(String exp)   {         if(exp.equals(“”)           return 0;        else           return 1 + task(exp.substring(1));    } } //from the Java 7 API documentation: public String substring(int...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT