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).

1  
!7
2  
3  
/**
4  
 * Takes a pattern string and an input string as command-line arguments;
5  
 * searches for the pattern string in the text string; and prints
6  
 * the first occurrence of the pattern string in the text string.
7  
 *
8  
 * @param args the command-line arguments
9  
 */
10  
public static void main(S[] args) {
11  
  tt();
12  
  
13  
  if (empty(args)) args = new S[] {"abc", "bcabc"};
14  
  String pat = args[0];
15  
  String txt = args[1];
16  
  char[] pattern = pat.toCharArray();
17  
  char[] text    = txt.toCharArray();
18  
19  
  BoyerMoore boyermoore1 = new BoyerMoore(pat);
20  
  BoyerMoore boyermoore2 = new BoyerMoore(pattern, 256);
21  
  int offset1 = boyermoore1.search(txt);
22  
  int offset2 = boyermoore2.search(text);
23  
24  
  // print results
25  
  println("text:    " + txt);
26  
27  
  printNoNewLine("pattern: ");
28  
  for (int i = 0; i < offset1; i++)
29  
      printNoNewLine(" ");
30  
  println(pat);
31  
32  
  printNoNewLine("pattern: ");
33  
  for (int i = 0; i < offset2; i++)
34  
      printNoNewLine(" ");
35  
  println(pat);
36  
}
37  
38  
39  
/**
40  
 *  The {@code BoyerMoore} class finds the first occurrence of a pattern string
41  
 *  in a text string.
42  
 *  <p>
43  
 *  This implementation uses the Boyer-Moore algorithm (with the bad-character
44  
 *  rule, but not the strong good suffix rule).
45  
 *  <p>
46  
 *  For additional documentation,
47  
 *  see <a href="https://algs4.cs.princeton.edu/53substring">Section 5.3</a> of
48  
 *  <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
49  
 *  Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne. 
50  
 *  Last updated: Fri Oct 20 12:50:46 EDT 2017.
51  
 */
52  
sclass BoyerMoore {
53  
  final int R;     // the radix
54  
  int[] right;     // the bad-character skip array
55  
56  
  char[] pattern;  // store the pattern as a character array
57  
  S pat;      // or as a string
58  
59  
  *(S pat) {
60  
    this.R = 256;
61  
    this.pat = pat;
62  
63  
    // position of rightmost occurrence of c in the pattern
64  
    right = new int[R];
65  
    for (int c = 0; c < R; c++)
66  
        right[c] = -1;
67  
    for (int j = 0; j < pat.length(); j++)
68  
        right[pat.charAt(j)] = j;
69  
  }
70  
71  
  /**
72  
   * Preprocesses the pattern string.
73  
   *
74  
   * @param pattern the pattern string
75  
   * @param R the alphabet size
76  
   */
77  
  public BoyerMoore(char[] pattern, int R) {
78  
      this.R = R;
79  
      this.pattern = new char[pattern.length];
80  
      for (int j = 0; j < pattern.length; j++)
81  
          this.pattern[j] = pattern[j];
82  
83  
      // position of rightmost occurrence of c in the pattern
84  
      right = new int[R];
85  
      for (int c = 0; c < R; c++)
86  
          right[c] = -1;
87  
      for (int j = 0; j < pattern.length; j++)
88  
          right[pattern[j]] = j;
89  
  }
90  
91  
  /**
92  
   * Returns the index of the first occurrrence of the pattern string
93  
   * in the text string.
94  
   *
95  
   * @param  txt the text string
96  
   * @return the index of the first occurrence of the pattern string
97  
   *         in the text string; n if no such match
98  
   */
99  
  public int search(String txt) {
100  
      int m = pat.length();
101  
      int n = txt.length();
102  
      int skip;
103  
      for (int i = 0; i <= n - m; i += skip) {
104  
          skip = 0;
105  
          for (int j = m-1; j >= 0; j--) {
106  
              if (pat.charAt(j) != txt.charAt(i+j)) {
107  
                  skip = Math.max(1, j - right[txt.charAt(i+j)]);
108  
                  break;
109  
              }
110  
          }
111  
          if (skip == 0) return i;    // found
112  
      }
113  
      return n;                       // not found
114  
  }
115  
116  
117  
  /**
118  
   * Returns the index of the first occurrrence of the pattern string
119  
   * in the text string.
120  
   *
121  
   * @param  text the text string
122  
   * @return the index of the first occurrence of the pattern string
123  
   *         in the text string; n if no such match
124  
   */
125  
  public int search(char[] text) {
126  
      int m = pattern.length;
127  
      int n = text.length;
128  
      int skip;
129  
      for (int i = 0; i <= n - m; i += skip) {
130  
          skip = 0;
131  
          for (int j = m-1; j >= 0; j--) {
132  
              if (pattern[j] != text[i+j]) {
133  
                  skip = Math.max(1, j - right[text[i+j]]);
134  
                  break;
135  
              }
136  
          }
137  
          if (skip == 0) return i;    // found
138  
      }
139  
      return n;                       // not found
140  
  }
141  
}

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: 375 / 841
Version history: 4 change(s)
Referenced in: [show references]