Java implementation of Boyer-moore algorithm

Source: Internet
Author: User

Boyer-moore Understanding Reference Blog

This article records the Java implementation of the Boyer-moore algorithm.

Import java.util.*;p Ublic class Boyermoore {public static void main (string[] args) {String text= "China is a great country; great Motherland"; String pattern= "The Great Kingdom"; Boyermoore bm=new Boyermoore (); Bm.boyermoore (pattern, text);} private void PREBMBC (String pattern,int patlength,map<string,integer> bmbc) {System.out.println ("BMBC start Process ... "); for (int i=patlength-2;i>=0;i--) {if (!bmbc.containskey (string.valueof (Pattern.charat (i))) { Bmbc.put (string.valueof (Pattern.charat (i)), (Integer) (patlength-i-1));}}} private void suffix (String pattern,int patlength,int [] suffix) {suffix[patlength-1]=patlength;int q=0;for (int i= patlength-2;i>=0;i--) {q=i;while (Q>=0&&pattern.charat (q) ==pattern.charat (patLength-1-i+q)) {q--;} Suffix[i]=i-q;}} private void Prebmgs (String pattern,int patlength,int []bmgs) {int I,j;int []suffix=new int[patlength];suffix (Pattern, Patlength,suffix);//The string does not match a good suffix, and a maximum prefix for (i=0;i<patlength;i++) {bmgs[i]=patlength is not found;} No substring in the pattern string matches the good suffix, but finds a maximum prefix j=0;for (i=patlength-1;i>=0;i--) {if (suffix[i]==i+1) {for (; j<patlength-1-i;j++) {if (bmgs[j]==patlength) {bmgs[j]=patlength-1-i;}}}} There are substrings in the pattern string that match the suffix for (i=0;i<patlength-1;i++) {bmgs[patlength-1-suffix[i]]=patlength-1-i;} System.out.print ("Bmgs:"); for (i=0;i<patlength;i++) {System.out.print (bmgs[i]+ ",");} System.out.println ();} private int GETBMBC (String c,map<string,integer> bmbc,int m) {//returns the length of the pattern if the corresponding value is returned in the rule if ( Bmbc.containskey (c)) {return bmbc.get (c);} Else{return m;}} public void Boyermoore (String pattern,string text) {int m=pattern.length (); int n=text.length (); Map<string,integer> bmbc=new hashmap<string,integer> (); int[] Bmgs=new INT[M];//PROPROCESSINGPREBMBC ( PATTERN,M,BMBC);p Rebmgs (pattern,m,bmgs)//searchingint j=0;int i=0;int count=0;while (j<=n-m) {for (i=m-1;i>=0 &&pattern.charat (i) ==text.charat (i+j); i--) {//For counting count++;} if (i<0) {System.out.println ("one position is:" +j); j+=bmgs[0];} Else{j+=math.max (BMGS[I],GETBMBC (string.valueof (Text.charat (i+j), bmbc,m)-m+1+i);}} System.out.println ("Count:" +count);}} 


Java implementation of Boyer-moore algorithm

Related Article

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.