String Matching--BM algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.