Question

In: Computer Science

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 S in sequence T using the Karp- Rabin algorithm. For each matched sequence, print the location of the first matched character in T.

For example, if the test data looked like this: ACGTACGTACCAGTA AC T
he output should be: 0, 4, 8
Notes: 1. The sequence T is no longer than 500 characters
2. The sequence S is no longer than 10 characters

has to be written in java

Solutions

Expert Solution

SOLUTION

CODE:

import java.util.Scanner;
import java.io.*;
public class StringMatching
{

public final static int SIZE = 500;

static void searchStr(String pat, String txt, int q)
{
int M = pat.length();
int N = txt.length();
int i, j;
int patt = 0;
int text = 0;
int h = 1;
  
  
for (i = 0; i < M-1; i++)
h = (h*SIZE)%q;
  
for (i = 0; i < M; i++)
{
patt = (SIZE*patt + pat.charAt(i))%q;
text = (SIZE*text + txt.charAt(i))%q;
}

for (i = 0; i <= N - M; i++)
{
  
if ( patt == text )
{
/* Check for characters one by one */
for (j = 0; j < M; j++)
{
if (txt.charAt(i+j) != pat.charAt(j))
break;
}
  
  
if (j == M)
System.out.println("Pattern found at index " + i);
}
  
if ( i < N-M )
{
text = (SIZE*(text - txt.charAt(i)*h) + txt.charAt(i+M))%q;
  
if (text < 0) //wrap for positive
text = (text + q);
}
}
}
  
public static void main(String[] args) throws FileNotFoundException
{
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter name of File");
   String filename=sc.nextLine();
sc=new Scanner(new File(filename));
       String line=sc.nextLine();
String txt[] =line.split(" "); // dividing at space, characters before space are text and after space are pattern. text--->txt[0] AND pat------> txt[1]
int q = 101; // A prime number
searchStr(txt[1], txt[0], q);
}
}

CODE SCREENSHOT

OUTPUT

FILE: input.txt

ACGTACGTACCAGTA AC


Related Solutions

Pattern matching using python3 re module Write a regular expression that will find out all the...
Pattern matching using python3 re module Write a regular expression that will find out all the words that ends with 4 consecutive vowels at the end. Example: import re f=open("/usr/share/dict/american-english",'r') pattern='a[a-z]*d$' words=f.readlines() matchlist=[word for word in words if re.match(pattern,word)] =============================== Generate random string follow a specific pattern You will take a username and ask user for the password. User has to enter a strong password in order to create his or her account. The criteria for strong password is, a)...
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
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 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, BRAVE,...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
2 Write a Java program called Pattern that prints an 8-by-8 checker box pattern using an...
2 Write a Java program called Pattern that prints an 8-by-8 checker box pattern using an if loop.
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab...
20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab you will code directly in ZyBooks. That means no uploading a file. If you wish, you can copy the template code into your IDE, work out a solution, and paste that into the code window. The problem The code does not work on certain data sets. Fix the sets but do not alter the binary search algorithm. The obvious Yes, this is a problem...
1.The binary bit pattern 10011. Please give the number for this pattern represent in each of...
1.The binary bit pattern 10011. Please give the number for this pattern represent in each of the following: ones complement integer, twos complement integer, unsigned integer, sign-magnitude integer, and fixed point factional number. (Please note that there will be two bits to the left of the binary points and three bits to the right)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT