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"