Java mode matching brute force matching
/*** mode matching brute force matching* /package javay.util; /*** Pattern match brut-leave* @Author DBJ* /PUBLIC CLASS PMBF { /*** Pattern Match BRUTE-FORCE*** @param target target String* @Param Pattern mode String* @Return mode String in the first place in the target series*/ Public Static Int Patternmatch (String Target, String Pattern) {int targetLength = Target.Length (); int PatternLength = Pattern. length (); int IDXTXTGT = 0; // The position of the character in the target string int idxptn = 0; // The position of the character in the mode string int index = 0; // Save the position of the starting character of the initial character of the mode string while (IDXTGT <TARGETLENGTH && IdXptn <PatternLength) {// Find a matching character if (target.charat (IDXTGT) == Pattern.charat (IDXPTN)) {// If equal to the character, IdxtGT + +; IDXPTN ++;} Else {// Otherwise, the target string starts with the first character of the second character to compare index ++; idXptn = 0; idxtGt = Index;} // Output results if (idxptn == patternlength) {// Instructions that match the successful Return Index;} else {return -1;}}}}
Example:
Static int indexof (char [] source, char [] target) {char fIRST = target [0]; int max = (source.Length -target.length); for (int i = 0; i <= max; i ++) { /* Look for flashter.* /If (source [i]! = First) {While (++ I <= Max && source [i]! = FIRST); Rest of v2 */ if (i <= max) {int j = i + 1; int end = j + target.length -1; for (int k = 1; j <end && source [j] == target [ k]; j ++, k ++); if (j == end) { / * Found WHOLE String. * / Return I;}}} Return -1;}
The above is all the contents of this article. I hope everyone can like it.