The BM algorithm is implemented through the Java language.
public class bfmatching {public static void main (string[] args) {long starttime;long endtime;long durationtime;starttime = System.nano Time (); BM (); endTime = System.nanotime ();d urationtime = Endtime-starttime; System.out.println (durationtime);} public static void BM () {String bm_s = "Ababcabcacbab"; String bm_t = "ABCAC"; int bmslength = Bm_s.length ();//main string length int bmtlength = Bm_t.length ();//substring length int i = Bmtlength-1;while (I <= bmslength) {for (int j = BMTLength-1; J >= 0;) {if (Bm_t.charat (j) = = Bm_s.charat (i)) {System.out.println (Bm_t.charat (j) + "= =" + Bm_s.charat (i)); if (j = = 0) {System.out . println ("Match success! "); return;} j--;i--;} else if (Bm_t.charat (j)! = Bm_s.charat (i)) {i = i + Dist (Bm_s.charat (i)); j = BMTLength-1;}}} public static int Dist (char c) {int Dist = 0;if (c = = ' a ') {Dist = 1;} else if (c = = ' B ') {Dist = 3;} else if (c = = ' C ') { dist = 5;} return dist;}}
Operation Result:
C==c
C==c
A==a
C==c
B==b
A==a
Match success!
655247
String Matching--BM algorithm