-
Notifications
You must be signed in to change notification settings - Fork 1
/
casamento.c
121 lines (94 loc) · 3.42 KB
/
casamento.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include "casamento.h"
void BMH(char *texto, int tamanhoTexto, char *padrao, int tamanhoPadrao, FILE* arq) {
fprintf(arq, " Casamento(s):");
if (tamanhoPadrao > tamanhoTexto) // se padrao > texto retorna pra main sem casamentos
return;
int i, j, k, d[MAXCHAR + 1];
for (i = 0; i <= MAXCHAR; i++) // pré - processamento
d[i] = tamanhoPadrao;
for (i = 1; i < tamanhoPadrao; i++)
d[padrao[i-1]] = tamanhoPadrao - i;
j = tamanhoPadrao;
while (j <= tamanhoTexto) { // buscando o padrão no texto
k = j;
i = tamanhoPadrao;
while (texto[k-1] == padrao[i-1] && i > 0) {
k--;
i--;
}
if (i == 0) // encontrou
fprintf(arq, " %d", k+1);
j += d[texto[j-1]];
}
}
void BMHS(char *texto, int tamanhoTexto, char *padrao, int tamanhoPadrao, FILE* arq) {
fprintf(arq, " Casamento(s):");
if (tamanhoPadrao > tamanhoTexto) // se padrao > texto retorna pra main sem casamentos
return;
int i, j, k, d[MAXCHAR + 1];
for (i = 0; i <= MAXCHAR; i++) // pré - processamento
d[i] = tamanhoPadrao + 1;
for (i = 1; i <= tamanhoPadrao; i++)
d[padrao[i-1]] = tamanhoPadrao - i + 1;
j = tamanhoPadrao;
while (j <= tamanhoTexto) { // buscando o padrão no texto
k = j;
i = tamanhoPadrao;
while (texto[k-1] == padrao[i-1] && i > 0) {
k--;
i--;
}
if (i == 0) // econtrou
fprintf(arq, " %d", k+1);
j += d[texto[j]];
}
}
void ShiftAnd(char *texto, int tamanhoTexto, char *padrao, int tamanhoPadrao, FILE* arq){
fprintf(arq, " Casamento(s):");
if (tamanhoPadrao > tamanhoTexto) // se padrao > texto retorna pra main sem casamentos
return;
int M[MAXCHAR], R = 0;
for (int i = 0; i < MAXCHAR; i++) // pré - processamento
M[i] = 0;
for (int i = 1; i <= tamanhoPadrao; i++)
M[padrao[i-1] + 127] |= 1 << (tamanhoPadrao - i);
for (int i = 0; i < tamanhoTexto; i++) { // busca pelo padrão no texto
R = ((R >> 1) | (1 << (tamanhoPadrao - 1))) & M[texto[i] + 127];
if ((R & 1) != 0){
if (i - tamanhoPadrao + 2 > 0) // encontrou
fprintf(arq, " %d", i - tamanhoPadrao + 2);
}
}
}
void ShiftAndAproximado(char *texto, int tamanhoTexto, char *padrao, int tamanhoPadrao, int k, FILE* arq){
if (k > tamanhoPadrao+1){ // não faz sentido ter um k maior que o tamanho do padrão
k = tamanhoPadrao+1; // logo se o usuário digitar um valor maior ele é ajustado
printf("Erro maior que o tamanho do padrão. Erro ajustado.\n", k);
}
fprintf(arq, " Casamento(s):");
int M[MAXCHAR], i, j, Ri, Rant, Rnovo;
int R[tamanhoPadrao + 1];
for (i = 0; i < MAXCHAR; i++) // pré - processamento
M[i] = 0;
for (i = 1; i <= tamanhoPadrao; i++)
M[padrao[i-1] + 127] |= 1 << (tamanhoPadrao - i);
R[0] = 0;
Ri = 1 << (tamanhoPadrao - 1);
for (i = 1; i <= k; i++)
R[i] = (1 << (tamanhoPadrao - i)) | R[i-1];
for (i = 0; i < tamanhoTexto; i++){ // casamento exato
Rant = R[0];
Rnovo = (((Rant) >> 1) | Ri) & M[texto[i] + 127];
R[0] = Rnovo;
for (j = 1; j <= k; j++) { // casamento aproximado
Rnovo = ((R[j] >> 1) & M[texto[i] + 127]) | Rant | (((Rant | Rnovo)) >> 1);
Rant = R[j];
R[j] = Rnovo | Ri;
}
if ((Rnovo & 1) != 0) // encontrou
if (i+1 > tamanhoPadrao-1)
fprintf(arq, " %d", i+1);
}
}