Пример #1
0
 // return offset of first match; N if no match
 public int search(char[] text) {
   int M = pattern.length;
   int N = text.length;
   int skip;
   for (int i = 0; i <= N - M; i += skip) {
     skip = 0;
     for (int j = M - 1; j >= 0; j--) {
       if (pattern[j] != text[i + j]) {
         skip = Math.max(1, j - right[text[i + j]]);
         break;
       }
     }
     if (skip == 0) return i; // found
   }
   return N; // not found
 }
Пример #2
0
 // return offset of first match; N if no match
 public int search(String txt) {
   int M = pat.length();
   int N = txt.length();
   int skip;
   for (int i = 0; i <= N - M; i += skip) {
     skip = 0;
     for (int j = M - 1; j >= 0; j--) {
       if (pat.charAt(j) != txt.charAt(i + j)) {
         skip = Math.max(1, j - right[txt.charAt(i + j)]);
         break;
       }
     }
     if (skip == 0) return i; // found
   }
   return N; // not found
 }