/* -------------------------------- Complete Boyer-Moore -------------------------------- */ import java.util.*; public class BM1 { public static int[] L; public static int[] l; public static void compute_L(int n) { int i, j; for ( i = 1; i <= n; i++ ) L[i] = 0; for ( j = 1; j <= n-1; j++ ) { i = n - (n-+1) + 1; L[i] = j; } } public static void compute_l() { } public static void compute_R() { } 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("====================================="); System.out.println("Trying:"); System.out.println(" " + "012345678901234567890"); System.out.println(" " + text); System.out.print(" "); for ( int k = 0; k < i-j; k++) System.out.print(" "); System.out.println(pattern); System.out.print(" "); for ( int k = 0; k < i-j; k++) System.out.print(" "); System.out.println("0123456789"); System.out.println(); System.out.print(" "); for ( int k = 0; k < i; k++) System.out.print(" "); System.out.println("^"); System.out.print(" "); for ( int k = 0; k < i; k++) System.out.print(" "); System.out.println("|"); System.out.println(); if (pattern.charAt(j) == text.charAt(i)) { if (j == 0) { System.out.println("********************"); System.out.println("MATCH !!!"); return i; // match } else { // looking-glass heuristic: proceed right-to-left i--; j--; } } else { // character jump heuristic if ( j < last[text.charAt(i)] ) { System.out.println("*** scanned pass last[c], jump 1 character...."); i = last_i + 1; } else { System.out.println("*** Use last() jump function"); System.out.println(" last[" + text.charAt(i) + "] = " + last[text.charAt(i)] ); i = i + m - 1 - last[text.charAt(i)]; } j = m - 1; last_i = i; System.out.println("After jump...."); System.out.println(" *** i = " + i); System.out.println(" *** j = " + j); System.out.println(); 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.println("Try"); System.out.println("T = tomxatexatomatoxxx"); System.out.println("P = tomato"); System.out.println(); 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); } }