-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathA KMP Application.cpp
49 lines (43 loc) · 1.06 KB
/
A KMP Application.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
48
49
/* You are given a text t and a pattern p. For each index of t, find
how many proper prefixes of p ends in this position. Similarly, find how many proper
suffixes start from this position.
While calculating the failure function, we can find for each position of the pattern p
how many of its own prefixes end in that position. After calculating that in dp[i],
we can just feel table[i] for text t.
*/
int pi[N], dp[N];
void prefixFun(string &p)
{
int now;
pi[0]=now=-1;
dp[0]=1; // 0th character is a prefix ending in itself, base case
for(int i=1; i<p.size(); i++)
{
while(now!=-1 && p[now+1]!=p[i])
now=pi[now];
if(p[now+1]==p[i]) pi[i]=++now;
else pi[i]=now=-1;
if(pi[i]!=-1) // calculate the # of prefixes end in this position of p
dp[i]=dp[pi[i]]+1;
else dp[i]=1;
}
}
int kmpMatch(string &p, string &t, int *table)
{
int now=-1;
FOR(i,0,t.size())
{
while(now!=-1 && p[now+1]!=t[i])
now=pi[now];
if(p[now+1]==t[i])
{
++now;
table[i]=dp[now]; // table for text t
}
else now=-1;
if(now+1==p.size())
{
now=pi[now];
}
}
}