// Textbook fragment 12.03 /** Simplified version of the Boyer-Moore (BM) algorithm, which uses * only the looking-glass and character-jump heuristics. * @return Index of the beginning of the leftmost substring of the text * matching the pattern, or -1 if there is no match. */ import java.util.*; public class BM2 { public static int BMmatch (String text, String pattern) { int[] last = buildLastFunction(pattern); int n = text.length(); int m = pattern.length(); int i = m -1; int last_i = i; printLast(last); if (i > n - 1) return -1; // no match if pattern is longer than text int j = m - 1; do { if (pattern.charAt(j) == text.charAt(i)) { System.out.print("P[" + j + "] = " + pattern.charAt(j)); System.out.print(" == "); System.out.print("T[" + i + "] = " + text.charAt(i)); System.out.println(); System.out.println(); if (j == 0) { return i; // match } else { // looking-glass heuristic: proceed right-to-left i--; j--; } } else { // character jump heuristic int help; System.out.println("*** last_i = " + last_i); System.out.print("P[" + j + "] = " + pattern.charAt(j)); System.out.print(" != "); System.out.print("T[" + i + "] = " + text.charAt(i)); System.out.println(); System.out.println("Jumping..."); System.out.println("text["+i+"] = " + text.charAt(i)); System.out.println(" m = " + m); System.out.println(" j = " + j); System.out.println(" 1 + last["+text.charAt(i)+"] = " + (1 + last[text.charAt(i)]) ); help = m - Math.min(j, 1 + last[text.charAt(i)]); System.out.println("Skip amount = " + help); if ( m - 1 - last[text.charAt(i)] > j ) i = i + m - 1 - last[text.charAt(i)]; else i = last_i + 1; last_i = i; j = m - 1; System.out.println("*** i = " + i); System.out.println("*** j = " + j); System.out.println(); } } while (i <= n - 1); return -1; // no match } public static int[] buildLastFunction (String pattern) { int[] last = new int[128]; // assume ASCII character set for (int i = 0; i < 128; i++) { last[i] = -1; // initialize array } for (int i = 0; i < pattern.length(); i++) { last[pattern.charAt(i)] = i; // implicit cast to integer ASCII code } return last; } public static void printLast(int[] last) { int i; System.out.println(); for (i = (int)'a'; i <= (int)'z'; i++) { System.out.print((char)i + " " + last[i] + "; "); if (i % 10 == 0) System.out.println(); } System.out.println(); System.out.println(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String T, P; int r; System.out.print("T = "); T = in.next(); System.out.print("P = "); P = in.next(); System.out.println(); System.out.println("012345678901234567890"); System.out.println(T); System.out.println("012345678901234567890"); System.out.println(P); System.out.println(); r = BMmatch (T, P); System.out.println("r = " + r); } }