/** * The simple string algorithm finds the sub -string through two layers of cycle. * It seems to be a "template" that contains patterns to slide the text. * The idea of the algorithm is: compares from the pattern string of the POS character of the main string S. When the matching is unsuccessful, the POS+1 character of the main string S is compared with the pattern string. * If the length of the main string S is n and the length of the mode string is m, then the time complexity of the BRUTE-FORCE is O (M* N). * The worst case appears in the sub -string of the mode string frequently appears in the main string S. * Although its time complexity is O (m * n), the matching time is O (m+n) in general, and * therefore is used in large quantities. * The advantages of this method are: the algorithm is simple and clear, which is convenient to achieve memory. * The disadvantage of this method is: the retrospective retrospective, the efficiency is not high, and these tracers are unnecessary. * Below is the Java code of the algorithm. If you find the sub -string, return the sub -string where the first appears in the parent string, * If you can't find it, return 0. */ Package Al; Public Class Brurece {Public Static Void Main ( String [] ARGS) {String WaitFormatch = "Abbacbabcdabcbec"; String Pattern = "Abcbe"; BRUTEFORCE BRUTEFORCE = New BRUTEFORCE (); Orce.getsubstringex (waitformatch, pattern); System.out.println ("Matched Index IS "+Index);} / *** @Author* @Param WaitFormatch main string* @param Pattern mode strings* @Return string matching successful position* / Public Int Getsubstringex (String WAI TFORMATCH, String Pattern) { int stringLength = waitForMatch.length(); int patternLength = pattern.length(); // 从主串开始比较for(int i=0; i<stringLength; i++) { int k = i; // k指向主String the next position for (Int j = 0; j <PatternLength; J ++) {if (waitformatch.charat (k)! = Pattern.charat (j)) {break;} else {k ++; Location if (j == patternlength-) {Return I;}}}} // Unsuccessful matching, return 0 Return 0;}}