Pattern Matching-bruteforce

Source: Internet
Author: User

The basic principle of pattern matching in the process of editing a text program, you often need to find all the occurrences of a certain pattern in the text. Typically, a piece of text being edited forms a file, the pattern to be searched is the specific keyword that the user is entering. The algorithm that effectively solves this problem is called the string matching pattern. Formal Description assume that the text is an array of N [1 .... n], while the pattern is an array of M [1... m], where M <n, further assuming that the P and T elements are from a finite letter set, so if 0 "S" N-M and T [S + 1... S + M] = P [1... m] So the pattern P appears in text t, and the offset is spackage jxau. bluedot. lyx;/*** @ liyixiang * @ 2014-8-16 * @ todo: * SIMPLE algorithm for pattern matching */public class bruteforce {/*** @ param1: substring * Main string t length is N * @ param2: childstring * sub-string P length is M * @ return * return offset S * // ***** 1. the first character of the string is compared with the first character of the Main string. If the first character of the string does not match the second character of the Main string, the second character of the Main string is compared * 2. if the first character of the string and If the string matches at a position, the second character of the string is compared with the next position of the main * string, and so on. If they are not equal, repeat the first step. */Public int stringmatching (string substr, string childstr) {int Index =-1; // If yes, the return offset is returned. If no value is returned, the return value is-1 Boolean match = true; // The offset from 0 to the maximum, that is, the length of the Main string. N-the length of the sub-string. mfor (INT I = 0; I <substr. length ()-childstr. length (); I ++) {match = true; For (Int J = 0; j <childstr. length (); j ++) {// if no matching result is found, if (substr. charat (I + J )! = Childstr. charat (j) {match = false ;}// if (MATCH) {Index = I; break ;}} Return Index ;}}


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.