-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguardando_strings.c
67 lines (54 loc) · 1.34 KB
/
guardando_strings.c
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
60
61
62
63
64
65
66
67
/*
* iagorrr ;)
* morris pratt algorithm (MP) to find the total occurrens of P as substring of S.
* O(n+m)
*/
#include <stdio.h>
#include <stdlib.h>
#define max(a,b) (a>b ? a : b)
int *border;
int *build_border(char *p, int m){
int *bd = (int*)malloc(sizeof(int)*(m+1));
for(int i = 0; i < m+1; ++i) bd[i] = -1;
int t = -1;
for(int i = 0; i < m; ++i){
while(t > -1 && p[t] != p[i])
t = bd[t];
bd[i+1] = ++t;
}
return bd;
}
int MP (char *s, int n, char *p, int m){
int i = 0;
int j = 0;
int occ = 0;
while(i <= n - m){
while(j < m && s[i+j] == p[j]) ++j;
if(j == m) ++occ;
int shift = j-border[j];
i += shift;
j = max(0, j-shift);
}
return occ;
}
int main(){
int *sizes = malloc(sizeof(int)*((int)1e5+66));
int i = 0;
int n;
int tsum = 0;
char *linguicao = (char*)malloc(sizeof(char) * ((1<<23)+(int)1e5*2) );
while(scanf("%s%n", &linguicao[tsum], &n) != EOF){
sizes[i] = n;
tsum += n+1;
++i;
scanf("%*c");
}
border = build_border(&linguicao[tsum-n], sizes[i-1]);
int cur = 0;
for(int j = 0; j < i-1; ++j){
printf("%d\n", MP(&linguicao[cur], sizes[j], &linguicao[tsum-sizes[i-1]-1], sizes[i-1]));
cur += sizes[j]+1;
}
return 0;
}
// AC.