Menu

(Solved) : 515 Suppose Want Find First Occurrence String P1p2 Pk Long Input String A1a2 Solve Proble Q26211733 . . .

5.15 Suppose we want to find the firstoccurrence 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 hashvalue formed from A1A2 ··· Ak, A2A3 ··· Ak+1, A3A4 ··· Ak+2, and soon until AN−k+1AN−k+2 ··· AN. If we have a match of hash values, wecompare the strings character by character to verify the match. Wereturn the position (in A) if the strings actually do match, and wecontinue in the unlikely event that the match is false.

*a. Show that if the hash value of AiAi+1 ···Ai+k−1 is known, then the hash value of Ai+1Ai+2 ··· Ai+k can becomputed in constant time.

b. Show that the running time is O(k + N) plusthe time spent refuting false matches.

*c. Show that the expected number of falsematches is negligible.

d. Write a program to implement thisalgorithm.

**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 (3rdEdition)  Chapter 5, 15E – Missing TextbookSolution

Expert Answer


Answer to 515 Suppose Want Find First Occurrence String P1p2 Pk Long Input String A1a2 Solve Proble Q26211733 . . .

OR