Sunday, January 15, 2017

Naive String Matching

This Algorithm/Code in Java performs naive string matching:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

package AdvancedAlgo;
import java.util.Scanner;

public class NaiveMethod {
private final int BASE;
private int[ ] occurrence;
private String pattern;
public int KeyCount; /** key operation counter **/

public NaiveMethod(String pattern) {
this.BASE = 256;
this.pattern = pattern;
KeyCount = 0;
occurrence = new int[BASE];

for (int c = 0; c < BASE; c++)
occurrence[c] = -1;
for (int j = 0; j < pattern.length(); j++)
occurrence[pattern.charAt(j)] = j;
}

public int search(String text) {
int n = text.length();
int m = pattern.length();
int skip;
for (int i = 0; i <= n - m; i += skip) {
skip = 0;
for (int j = m - 1; j >= 0; j--) {
KeyCount++;
if (pattern.charAt(j) != text.charAt(i + j)) {
skip = Math.max(1, j - occurrence[text.charAt(i + j)]);
break;
}
}
if (skip == 0)
return i;
}
return n;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the main string: ");
String text = sc.nextLine();
System.out.print("Enter the string to be searched: ");
String pattern = sc.nextLine();
NaiveMethod nsm = new NaiveMethod(pattern);
int first_occur_position = nsm.search(text);
System.out.println("The text '" + pattern + "' is first found after the " + first_occur_position
+ " position.");
System.out.println("\nNo of comparisons : " + nsm.KeyCount );
sc.close();
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Input / Output:

Enter the main string: Hello How are You
Enter the string to be searched: are
The text 'are' is first found after the 10 position.

No of comparisons : 7

No comments:

Post a Comment