-
Notifications
You must be signed in to change notification settings - Fork 3
/
kmp.cpp
47 lines (43 loc) · 874 Bytes
/
kmp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;
int pf[100000];
void ComputePrefixFunction(int m,char pattern[])
{
pf[1] = 0;
int k = 0;
for (int q = 2; q <= m ; q++)
{
while (k > 0 && pattern[k] != pattern[q - 1])
k = pf[k];
if (pattern[k] == pattern[q - 1])
k++;
pf[q] = k;
}
}
int KMP(char text[], int n, int m, char pattern[])
{
int count = 0;
for (int i = 1; i <= n; i++)
{
while (count > 0 && pattern[count] != text[i-1])
count = pf[count];
if (pattern[count] == text[i-1])
count++;
if (count == m)
return i - m;
}
return -1;
}
int main()
{
memset(pf, 0, sizeof(pf));
char text[100000], pattern[100000];
cin >> text >> pattern;
int n = strlen(text), m = strlen(pattern);
ComputePrefixFunction(m, pattern);
cout << KMP(text, n, m, pattern)<<endl;
}