Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

141
LINES

< > BotCompany Repo | #1013279 // Boyer Moore Spike (fast string search)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (1925L/12K).

!7

/**
 * Takes a pattern string and an input string as command-line arguments;
 * searches for the pattern string in the text string; and prints
 * the first occurrence of the pattern string in the text string.
 *
 * @param args the command-line arguments
 */
public static void main(S[] args) {
  tt();
  
  if (empty(args)) args = new S[] {"abc", "bcabc"};
  String pat = args[0];
  String txt = args[1];
  char[] pattern = pat.toCharArray();
  char[] text    = txt.toCharArray();

  BoyerMoore boyermoore1 = new BoyerMoore(pat);
  BoyerMoore boyermoore2 = new BoyerMoore(pattern, 256);
  int offset1 = boyermoore1.search(txt);
  int offset2 = boyermoore2.search(text);

  // print results
  println("text:    " + txt);

  printNoNewLine("pattern: ");
  for (int i = 0; i < offset1; i++)
      printNoNewLine(" ");
  println(pat);

  printNoNewLine("pattern: ");
  for (int i = 0; i < offset2; i++)
      printNoNewLine(" ");
  println(pat);
}


/**
 *  The {@code BoyerMoore} class finds the first occurrence of a pattern string
 *  in a text string.
 *  <p>
 *  This implementation uses the Boyer-Moore algorithm (with the bad-character
 *  rule, but not the strong good suffix rule).
 *  <p>
 *  For additional documentation,
 *  see <a href="https://algs4.cs.princeton.edu/53substring">Section 5.3</a> of
 *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
 *  Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne. 
 *  Last updated: Fri Oct 20 12:50:46 EDT 2017.
 */
sclass BoyerMoore {
  final int R;     // the radix
  int[] right;     // the bad-character skip array

  char[] pattern;  // store the pattern as a character array
  S pat;      // or as a string

  *(S pat) {
    this.R = 256;
    this.pat = pat;

    // position of rightmost occurrence of c in the pattern
    right = new int[R];
    for (int c = 0; c < R; c++)
        right[c] = -1;
    for (int j = 0; j < pat.length(); j++)
        right[pat.charAt(j)] = j;
  }

  /**
   * Preprocesses the pattern string.
   *
   * @param pattern the pattern string
   * @param R the alphabet size
   */
  public BoyerMoore(char[] pattern, int R) {
      this.R = R;
      this.pattern = new char[pattern.length];
      for (int j = 0; j < pattern.length; j++)
          this.pattern[j] = pattern[j];

      // position of rightmost occurrence of c in the pattern
      right = new int[R];
      for (int c = 0; c < R; c++)
          right[c] = -1;
      for (int j = 0; j < pattern.length; j++)
          right[pattern[j]] = j;
  }

  /**
   * Returns the index of the first occurrrence of the pattern string
   * in the text string.
   *
   * @param  txt the text string
   * @return the index of the first occurrence of the pattern string
   *         in the text string; n if no such 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
  }


  /**
   * Returns the index of the first occurrrence of the pattern string
   * in the text string.
   *
   * @param  text the text string
   * @return the index of the first occurrence of the pattern string
   *         in the text string; n if no such 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
  }
}

Author comment

from https://algs4.cs.princeton.edu/53substring/BoyerMoore.java.html

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1013279
Snippet name: Boyer Moore Spike (fast string search)
Eternal ID of this version: #1013279/5
Text MD5: 1c62aca8e1200239a323252e7abf5999
Transpilation MD5: 5b379643d917f57559bcc39dcc9d1fac
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-31 06:56:06
Source code size: 4192 bytes / 141 lines
Pitched / IR pitched: No / No
Views / Downloads: 366 / 821
Version history: 4 change(s)
Referenced in: [show references]