Question

In: Computer Science

You have to perform the following tasks: 1- Write the name of best string matching algorithm...

You have to perform the following tasks:

1- Write the name of best string matching algorithm in terms of time and space complexity.

2- Implement it in (c or c++)

3- Dry run it on following strings:

- String A=aaabaaaabbaaabcaaabd

- String B=aaabd

Deliverable in PDF file:

Code + Dry run + Code execution results using the above given strings.

Solutions

Expert Solution

ANSWER :-

(1). All string matching are good, it's all depends on what kind of search operation you are performing but in generally best algorithm for pattern searching is Knuth Morris Pratt (KMP) algrorithm ("In terms of its time and space complexity").

Time Complexity = O(n)

(2).

#include <iostream>
using namespace std; 
// implementing KMP algorithm
void KMPAlgo(string A, string B)
{
    int x = A.length();
    int y = B.length();
 
    // if B(Sub String) is an empty string
    if (y == 0)
    {
        cout << "Pattern found at index 0";
        return;
    }
 
    // if A's length is less than that of B's
    if (x < y)
    {
        cout << "Pattern not found";
        return;
    }
 
    // next[i] stores the index of next best partial match.
    int next[y + 1];
 
    for (int i = 0; i < y + 1; i++)
        next[i] = 0;
 
    for (int i = 1; i < y; i++)
    {
        int j = next[i + 1];
 
        while (j > 0 && B[j] != B[i])
            j = next[j];
 
        if (j > 0 || B[j] == B[i])
            next[i + 1] = j + 1;
    }
 
    for (int i = 0, j = 0; i < x; i++)
    {
        if (A[i] == B[j])
        {
            if (++j == y)
                cout << "Pattern found at index " << i - j + 1 << endl;
        }
        else if (j > 0) {
            j = next[j];
            i--;    // since i will be incremented in next iteration
        }
    }
}
 
//main function
int main()
{
    string String = "aaabaaaabbaaabcaaabd";
    string SubString = "aaabd";
 
    KMPAlgo(String, SubString);
 
    return 0;
}

(3).

String A=aaabaaaabbaaabcaaabd

String B=aaabd

Output :-


Related Solutions

Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Description: Write and test a MASM program to perform the following tasks: 1. Display your name...
Description: Write and test a MASM program to perform the following tasks: 1. Display your name and program title on the output screen. 2. Display instructions for the user. 3. Prompt the user to enter two numbers. 4. Calculate the sum, difference, product, (integer) quotient and remainder of the numbers. 5. Display a terminating message. Requirements: 1. The main procedure must be divided into sections:  introduction  get the data  calculate the required values  display the results...
Program Description Write and test a MASM program to perform the following tasks: Display your name...
Program Description Write and test a MASM program to perform the following tasks: Display your name and program title on the output screen. Display instructions for the user. Prompt the user to enter three numbers (A, B, C) in descending order. Calculate and display the sum and differences: (A+B, A-B, A+C, A-C, B+C, B-C, A+B+C). Display a closing message. Program Requirements The program must be fully documented and laid out according to the CS271 Style Guide. This includes a complete...
Objectives  To apply pattern matching algorithm concepts.  To write the program to represent a...
Objectives  To apply pattern matching algorithm concepts.  To write the program to represent a Karp-Rabin method Problem You are to implement the Karp-Rabin string search algorithm. Your program will prompt for the name of an input file and then read and process the data contained in this file. The file contains two sequences of characters. The first is the target sequence, T, the second is the search sequence S. Read both strings and find all occurrences of sequence...
Write a program with class name ForLoops that uses for loops to perform the following steps:1....
Write a program with class name ForLoops that uses for loops to perform the following steps:1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smallerthan secondNum).2. Output all the even numbers between firstNum and secondNum inclusive.3. Output the sum of all the even numbers between firstNum and secondNum inclusive.4. Output all the odd numbers between firstNum and secondNum inclusive.5. Output the sum of all the odd numbers between firstNum and secondNum inclusive. Java programming
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
Based on the tables below, write SQL command to perform the following tasks for MySql: Create...
Based on the tables below, write SQL command to perform the following tasks for MySql: Create SALESREP and CUSTOMER tables Create primary and foreign keys as appropriate. The custNo should use a surrogate key as the primary key with auto-increment increase the balance of the Gonzales account by $100 to a total of $450? Find an average customer balance Display the name of the sales representative and the name of the customer for each customer that has a balance greater...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String),...
Write the following classes: Class Entry: 1. Implement the class Entry that has a name (String), phoneNumber (String), and address (String). 2. Implement the initialization constructor . 3. Implement the setters and getters for all attributes. 4. Implement the toString() method to display all attributes. 5. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. 6. Implement the compareTo (Entry...
Part 1 readFile(String filename) In this method you are passed a String with the name of...
Part 1 readFile(String filename) In this method you are passed a String with the name of a file. This method will read the file in line by line and store each line in a String array. This String array is then returned. An example is shown below. File Contents: Purple Rain by Prince I never meant to cause you any sorrow I never meant to cause you any pain I only wanted one time to see you laughing I only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT