public static int search(String pattern, String text, int[] next) {
      int i, j;
      @SuppressWarnings("unused")
      int count = 0;
      for (i = 0, j = 0; i < text.length(); i++) {
        while (j < pattern.length()) {
          while (j >= 0 && text.charAt(i) != pattern.charAt(j)) {
            j = next[j];
          }
          j++;
        }
        count++;
        j = 0;
      }
      if (j == pattern.length())
        return i - pattern.length();
      return -1;
    }