Created
October 6, 2011 21:21
-
-
Save mejibyte/1268711 to your computer and use it in GitHub Desktop.
My implementation of the Knuth-Morris-Pratt algorithm in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.spoj.pl/problems/NHAY/ | |
#include <vector> | |
#include <iostream> | |
#include <string> | |
#include <cstdio> | |
using namespace std; | |
void kmp(const string &needle, const string &haystack) { | |
int m = needle.size(); | |
vector<int> border(m + 1); | |
border[0] = -1; | |
for (int i = 0; i < m; ++i) { | |
border[i+1] = border[i]; | |
while (border[i+1] > -1 and needle[border[i+1]] != needle[i]) { | |
border[i+1] = border[border[i+1]]; | |
} | |
border[i+1]++; | |
} | |
int n = haystack.size(); | |
int seen = 0; | |
for (int i = 0; i < n; ++i){ | |
while (seen > -1 and needle[seen] != haystack[i]) { | |
seen = border[seen]; | |
} | |
if (++seen == m) { | |
printf("%d\n", i - m + 1); | |
seen = border[m]; // There are no more characters in needle, so with the next input character let's try with the border of the whole needle. | |
} | |
} | |
} | |
int main(){ | |
int m; | |
bool first = true; | |
while (cin >> m) { | |
if (!first) puts(""); | |
first = false; | |
string needle; getline(cin, needle); | |
getline(cin, needle); | |
string haystack; | |
getline(cin, haystack); | |
kmp(needle, haystack); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment