// 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. */ /* -------------------------------- Code simplified by S.Y.Cheung -------------------------------- */ import java.util.*; public class BM3 { 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 { System.out.println("Trying:"); System.out.println(" " + text); System.out.print(" "); for ( int k = 0; k < i-j; k++) System.out.print(" "); System.out.println(pattern); System.out.println(); if (pattern.charAt(j) == text.charAt(i)) { if (j == 0) { return i; // match } else { // looking-glass heuristic: proceed right-to-left i--; j--; } } else { // character jump heuristic System.out.println("*** last_i = " + last_i); if ( j < last[text.charAt(i)] ) { System.out.println("*** scanned pass last[c], jump 1 character...."); i = last_i + 1; } else { System.out.println("*** Normal jump"); i = i + m - 1 - last[text.charAt(i)]; } j = m - 1; last_i = i; System.out.println("Jump...."); 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); } }