Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem Description Bob and Alice are competitive word-search puzzle solvers. However, even to the best of Bob's abilities, he was never able to find words
Problem Description Bob and Alice are competitive word-search puzzle solvers. However, even to the best of Bob's abilities, he was never able to find words in the puzzles faster than Alice. As Bob's friend and amazing programmer, you have decided to help him out by creating a solver for word-search puzzles A word-search puzzle is solved when, given a grid of random letters and a set of words, the program finds the location of the input words in the grid (if they exist). The orientation of the words can be horizontal, vertical, left diagonal, or right diagonal. The words can be read left to right, right to left, top to bottom (whether vertically or diagonally), and bottom to up (whether vertically or diagonally). Note that a diagonal can start at any index, not necessarily on a central diagonal You have decided to approach the problem using the Rabin-Karp algorithm. The Rabin-Karp algorithm uses what is called a hash function to find a substring of size x in a string of size y x The hash function is applied as follows Decide on a prime number p For a substring of size x, the hash function multiplies the ASClIl value of the first letter by pMx-1), adds it to the ASCII value of the second letter multiplied by ^(x-2), and so on until the ASCII code of the last letter is multiplied by p^(0). For example, calculated as hash ( "hello" ) 80976 with p#5 . This is = hash ( "hello" ) h* ( 5^4) e*(53) 1*(52) + 1"(51) o* ( 5"O) 104*625 101+125 + 108*25 108*5 111*1 80976 = + + + = + + + The Rabin-Karp algorithm applies this hash function to all substrings of a specific size in a given string, storing the calculated hash value for each applicable index of the given string. The hash value is stored at the index corresponding to the start of the substring being hashed. The number of stored hashes depends on the length of the given string and the length of the substring being searched for. If the algorithm cannot make a hash value with the specified length of the word, it counts those positions as 0.For example, assume the algorithm is searching for the word oworl in the string helloworld .Since the input word is of length 5, it will calculate the hash values of all substrings of length 5. In this case, it will calculate 10 hash values: the hash value of the word hello at index 0, the hash value for the word ellow at index 1, the hash value for the word llowo at index 2, the hash value for the word lowor at index 3, the hash value of the word oworl at index 4, the hash value of the word world at index 5, and the value 0 in indices 6-9. While searching for a word, if any of the values match the hash value of the given word, the algorithm has found the position of the start of the word. The nice thing about Rabin-Karp is that it constructs consecutive hashes in O(1) time. This is done by taking the previous hash value, subtracting from it the hash value of the first letter of the previous hash, multiplying by the prime p, and adding the value of the new letter. For example, the hash value for the word hello with a prime p=5 is 80976. To get the hash value for the next substring of length x (x=5 in this case) ellow , we can simply remove the ASCII value for h (104) multiplied by p^(x-1), then multiply by p, then add the ASCII value of w (119). So the hash( ellow ) = ( 80976-104 * 5^(4) ) * 5 + 119 = 79999 For the purposes of this assignment, you must fix p to 101 Task Write a program that uses the Rabin-Karp algorithm to find the location of individual words in a given word-search grid and outputs their starting position and direction, if they exist. Enumerate the directions as follows: . horizontal right () 2. horizontal left () 3. vertical down (1) 4. vertical up (t) 5. top left to bottom right diagonal 6. bottom right backwards to top left diagonal () 7. bottom left to top right() 8. top right backwards to bottom left) How to run your program Your program runs as follows: ./wordSearch2D -p
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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