Question

Modify the Rabin-Karp algorithm to search for a given pattern with the additional provision that some character in the patter

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

source code

#include<iostream>
#define MAXCHAR 256
using namespace std;

void rabinKarpSearch(string mainString, string pattern, int prime, int array[], int *index) {
   int patLen = pattern.size();
   int strLen = mainString.size();
   int charIndex, pattHash = 0, strHash = 0, h = 1;

   for(int i = 0; i<patLen-1; i++) {
      h = (h*MAXCHAR) % prime;    //calculating h = {d^(M-1)} mod prime
   }
   
   for(int i = 0; i<patLen; i++) {
      pattHash = (MAXCHAR*pattHash + pattern[i]) % prime;    //pattern hash value
      strHash = (MAXCHAR*strHash + mainString[i]) % prime;   //hash for first window
   }
   
   for(int i = 0; i<=(strLen-patLen); i++) {
      if(pattHash == strHash) {      //when hash values are same check for matching
         for(charIndex = 0; charIndex < patLen; charIndex++) {
            if(mainString[i+charIndex] != pattern[charIndex])
               break;
         }

         if(charIndex == patLen) {    //the pattern is found
            (*index)++;
            array[(*index)] = i;
         }
      }

      if(i < (strLen-patLen)) {    //find hash value for next window
         strHash = (MAXCHAR*(strHash - mainString[i]*h) + mainString[i+patLen])%prime;
         if(strHash < 0) {
            strHash += prime;    //when hash value is negative, make it positive
         }
      }
   }
}

int main() {
   string mainString = "ABAAABCDBBABCDDEBCABC";
   string pattern = "ABC";
   int locArray[mainString.size()];
   int prime = 101;
   int index = -1;
   rabinKarpSearch(mainString, pattern, prime, locArray, &index);

   for(int i = 0; i <= index; i++) {
      cout << "Pattern found at position: " << locArray[i]<<endl;
   }
}
Add a comment
Know the answer?
Add Answer to:
Modify the Rabin-Karp algorithm to search for a given pattern with the additional provision that ...
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
  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • 4. [30 pts; 10+9+11] a) You are given traffic data (number of p large number, in the order of hundreds of thousands.) with greatest amount of traffic (k is much smaller than n). Which of inserti...

    4. [30 pts; 10+9+11] a) You are given traffic data (number of p large number, in the order of hundreds of thousands.) with greatest amount of traffic (k is much smaller than n). Which of insertion sort, m quicksort, or heapsort may be used to devise the fastest (in worst case) algorithm for this task? What would be its worst case big O running time, in terms of n and k? (Note: it as soon as you get the top...

  • Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Plea...

    Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Please do not use 'print' or 'input' statements. Context of the assignment is: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. Please see attachments below: We need to consider the following cases: Case 1 - It is possible that the...

  • You will be given several strings full of different keyboard characters, some of which are letters...

    You will be given several strings full of different keyboard characters, some of which are letters of the alphabet. Write a java program that creates a binary tree that uses only the alphabetical characters (a-z, A-Z) as the value within the leaf nodes using recursion and preorder traversal. All other values within the tree (either the root node or internal nodes) will be null. You may create any methods you see fit, so long as the final binary tree is...

  • 2. (10 points.) The procedure HASH-SEARCH takes a hash table T[O : m-1) and a key...

    2. (10 points.) The procedure HASH-SEARCH takes a hash table T[O : m-1) and a key k as its parameters. Each element of T is either a key or NIL. The procedure searches for k in T by probing. If it finds k, then it returns the index j where k appears in T. If it cannot find k, then it returns -1. (A procedure very much like HASH-SEARCH is discussed on pages 269–274 of Cormen.) HASH-SEARCH(T, k) i= 0...

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