String Matching--BF algorithm

Source: Internet
Author: User

Implemented with Java code!

public class Matching {public static void main (string[] args) {BF ();} public static void BF () {String bf_s = "Aaaabbbaab";//main string string bf_t = "Aaab";//pattern string int bfslength = Bf_s.length ();//main string length int Bftlength = Bf_t.length ();//pattern string length for (int i = 0; i < bfslength;) {for (int j = 0; J < Bftlength;) {if (Bf_s.charat (i) = = Bf_t.charat (j)) {//Cycle compares the main string to the pattern string for equality System.out.println (Bf_s.charat (i) + "= =" + Bf_t.charat (j) + ""); /* If the main string matches the pattern string and is the last character of the pattern string, the match succeeds */if (Bf_s.charat (i) = = Bf_t.charat (j) && Bf_t.charat (j) = = Bf_t.charat ( BFTLength-1) {System.out.println ("Match succeeded! "); return;} i++;j++;} else if (Bf_s.charat (i)! = Bf_t.charat (j)) {int count = i + 1; System.out.println ("n" + Count + "time of comparison"); System.out.println (Bf_s.charat (i) + "! =" + Bf_t.charat (j)); i = i-j + 1;//Emphasis, if unequal, the main string moves from the beginning of the previous comparison to the next position compared to the starting position of the pattern string j = 0 (///pattern string starting from the starting position}}}}}

Operation Result:


String Matching--BF 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.