-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathz-algorithm.cpp
59 lines (53 loc) · 1.41 KB
/
z-algorithm.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
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
#define MAX_PATTERN_LENGTH 1000
#define MAX_TEXT_LENGTH 1000000
int z[MAX_PATTERN_LENGTH + MAX_TEXT_LENGTH + 1];
void computeZ(const char* s, int length){
int lp, rp, k;
z[0] = -1; lp = rp = 0;
for( int i = 1; i < length; i++ ){
if(rp < i){
lp = rp = i;
while(rp < length && s[rp - i] == s[rp])
rp++;
rp--; z[i] = rp - lp + 1;
}
else{
k = i - lp;
if( z[k] < rp - i +1 )//<=
z[i] = z[k];
else{
lp = i;
//rp = i + z[k] ;
while(rp < length && s[rp - i] == s[rp])
rp++;
rp--;z[i] = rp - lp + 1;
}
}
}
}
/*
Assume z is computed over P$T string where $ is not present in P and T
*/
void showPatternPosition(int length, int patLength){
for(int i = 0; i <= length; i++)
if(z[i] == patLength)
printf("%d ", i - patLength - 1);
}
int main(){
char s[MAX_PATTERN_LENGTH + MAX_TEXT_LENGTH + 1];
char pat[MAX_PATTERN_LENGTH],text[MAX_TEXT_LENGTH];
int M, N;
while(gets(text)){
gets(pat);
N = strlen(text);
M = strlen(pat);
strcpy(s,pat);
s[M] = '$';s[M+1] = NULL;
strcat(s,text);
computeZ(s, M + N + 1);
showPatternPosition(M+N+1, M);
}
return 0;
}