Question
5.15 Suppose we want to find the first occurrence of a string P1P2 Pk in a long input string A1A2 AN. We can solve this
5.15 Suppose we want to find the first occurrence of a string P1P2 Pk in a long input string A1A2 AN. We can solve this problem by hashing the pattern string, obtaining a hash value HP, and comparing this value with the hash value formed from A1A2 Ak, A2A3 Ak+1, A3A4 Ak+2, and so on until ANk+1ANk+2 AN. If we have a match of hash values, we compare the strings character by character to verify the match. We return the position (in A) if the strings actually do match, and we continue in the unlikely event that the match is false.
*a. Show that if the hash value of AiAi+1 Ai+k1 is known, then the hash value of Ai+1Ai+2 Ai+k can be computed in constant time.
b. Show that the running time is O(k + N) plus the time spent refuting false matches.
*c. Show that the expected number of false matches is negligible.
d. Write a program to implement this algorithm.
**e. Describe an algorithm that runs in O(k + N) worst-case time.
**f. Describe an algorithm that runs in O(N/k) average time.
Data Structures and Algorithm Analysis in Java (3rd Edition) Chapter 5, 15E - Missing Textbook Solution
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started