str_matching.h
Go to the documentation of this file.
1 // Document this file
4 # pragma once
5 
6 #include <string>
7 #include <cmath>
8 #include <climits>
9 
10 #include <iostream>
11 using namespace std;
12 
24 namespace alglib {
25 namespace string {
26 
27 size_t naive_str_matching(const std::string& text, const std::string& pattern) {
34  int s = 0;
35  int n = text.length(), m = pattern.length();
36  for(s = 0; s <= n - m; s++)
37  // Loop Invariant : The pattern doesn't exist in T[0:s-1] but may exist
38  // in T[s, n].
39  if(text.substr(s, m) == pattern)
40  return s;
41 
42  return std::string::npos;
43 
66 }
67 
68 // Helper procedure for rabin-karp algorithm
69 static long int poly_hash(const std::string& input, int m) {
70  const int radix = 10;
71  const long int Q = 997;
72  long int hash_val = 0;
73  for(int i = 0; i < m; i++)
74  hash_val = (hash_val * radix + input[i]) % Q;
75  return hash_val;
76 }
77 
78 size_t rabin_karp(const std::string& text, const std::string& pattern) {
86  int m = pattern.length();
87  int n = text.length();
88  const long int R = 10;
89  long int pattern_hash = poly_hash(pattern, m);
90  long int text_hash = poly_hash(text, m);
91  long int R_m_minus_1 = std::pow(R, m - 1);
92 
93  if(pattern_hash == text_hash)
94  if(pattern == text.substr(0, m))
95  return 0;
96  for(int s = 1; s <= n - m; s++) {
97  text_hash = (text_hash - R_m_minus_1 * text[s-1]) * R + text[s + m - 1];
98  if(pattern_hash == text_hash)
99  if(pattern == text.substr(s, m))
100  return s;
101  }
102 
103  return std::string::npos;
104 }
105 
106 
107 
108 
109 size_t horspool(const std::string& text, const std::string& pattern) {
110 
111  int skip_table[UCHAR_MAX + 1];
112  int n = text.length(), m = pattern.length();
113 
114  // Initialize each of the index of the shift table to m.
115  std::fill_n(skip_table, UCHAR_MAX + 1, m);
116 
117  // Compute the skip table for the given pattern.
118  for(int i = 0; i < m - 1; i++) {
119  skip_table[pattern[i]] = m - i - 1;
120  }
121 
122  // Do the string comparison
123 
124  // align the pattern shifted with s in [0, n-m] with the text
125  for(int shift = 0; shift <= n - m;) {
126 
127  // For every shift, start comparing from back
128  int i = m - 1;
129 
130  // Keep moving to the right until a mismatch is detected.
131  while(i >= 0 && (text[shift + i] == pattern[i]))
132  i--;
133 
134  if(i == -1)
135  // if the pattern matched completely with the text
136  return shift;
137 
138  shift += skip_table[text[shift + m - 1]];
139  }
140 
141  return std::string::npos;
142 
143 }
144 
151 size_t kmp(const std::string& text, const std::string& pattern) {
152 
153 
154 }
155 
156 
157 
158 } // end string namespace
159 } // end alglib namespace
size_t naive_str_matching(const std::string &text, const std::string &pattern)
Definition: str_matching.h:27
size_t rabin_karp(const std::string &text, const std::string &pattern)
Definition: str_matching.h:78
Definition: bimap.h:17
size_t horspool(const std::string &text, const std::string &pattern)
Definition: str_matching.h:109
size_t kmp(const std::string &text, const std::string &pattern)
Performs substring search in a given string.
Definition: str_matching.h:151