[Basis of algorithm design and analysis] 6. Brute Force Character matching, algorithm design and analysis matching
Package com. cutterpoint. gogogo. program ;/****. ** @ copyright: copyright of Fufu software (c) 2015 * @ author xiaof * @ version Revision 1.0.0 * @ see: * @ creation Date: July 15, March 15, 2017 * @ function description: **/public class BruteForceStringMatch {public static int pipei (String t, String p) {// cyclically traverse to t. the number of times the length-p.length int cha = t. length ()-p. length (); char tt [] = t. toCharArray (); char pp [] = p. toCharArray (); for (int I = 0; I <= cha; ++ I) {// matches the circular traversal mode int j = 0; while (j <p. length () & tt [I + j] = pp [j]) {// If the match is successful, the overall backward movement + + j; // if j ++ is followed by the end of the pattern, the return position if (j = p. length () return I; // starting from the position I, the pattern matches the first substring OK} return-1;} public static void main (String [] args) {String t = "cutterpoint"; String p = "point"; System. out. println (BruteForceStringMatch. pipei (t, p ));}}
The efficiency of this method obviously cannot be directly looked at, and there will be corresponding improvements later, such as the kmp Algorithm