Algorithm Note _009: string matching "brute force method"

Source: Internet
Author: User

1 problem description

given a string of n characters (called text), a string of m(m <= n), called a pattern, looks for a substring of the matching pattern from the text.

2 Solutions 2.1 specific code
 PackageCom.liuzhen.chapterThree; Public classBruteforcestringmatch {//returns the position of the substring of the first matched pattern string in N, based on the text string n, and the pattern string m     Public Static intGetstringmatch (int[] N,int[] M) {        intn = n.length;//length of text string        intm = m.length;//the length of the pattern string         for(inti = 0;i < n-m;i++) {//The starting position of the last wheel string match is n-m, and the matching substring will not appear if it is greater than n-m            intj = 0;  while(J < m && M[j] = = n[i+J]) J= J +1; if(J = =m)returni; }        return-1; }         Public Static voidMain (String args[]) {int[] N = {1,2,3,2,4,5,6,7,8, 9}; int[] M = {6,7,8}; intPosition =Getstringmatch (n,m); System.out.println (The "+position+" bit in the text string n begins with the search for a substring that matches the pattern m, where the character value is: "+n[position]); }}

2.2 running results
Beginning with the 6th bit in the text string n, you can find a substring that matches the pattern m, where the character value is: 6

Algorithm Note _009: string matching "brute force method"

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.