String Pattern Matching-Brute-Force Algorithm

Source: Internet
Author: User

The basic idea of the Brute-Force algorithm is: from the target string s = "s0s1... the first character of the sn-1 starts and the pattern string t = "t0t1t2... the first character comparison in the tn-1, if equal, continues to compare subsequent characters one by one; otherwise recompares from the second character of the target string to the first character of the pattern string t. Similarly, if each character starting with the I character of the mode string s is the same as the corresponding character in the t character of the target string, the match is successful and I is returned. Otherwise, the match fails, the algorithm returns-1.

Int index (SqString s, SqString t) {int I = 0, j = 0; while (I <s. length & j <t. length) {if (s. data [I] = t. data [j]) {I ++; j ++;} else {I = I-j + 1; j = 0 ;}} if (j> = t. length) return (i-t.length); else return-1 ;}

 


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.