- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitPattern.c
More file actions
Latest commit
188 lines (177 loc) · 6 KB
/
Copy pathBitPattern.c
File metadata and controls
188 lines (177 loc) · 6 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Implementazione dell'Algoritmo BitPattern per il PatternMatching.
L'algoritmo controlla quante volte il pattern P viene ripetuto nel testo T.
(Si presuppone un tempo di esecuzione O(n)).
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"Lunghezza.h"
/* Prototipi di funzioni:*/
voidbuildArray(intar[], char*testo, chark); // costruisce i vettori Ux settando a 1 le posizioni in cui x compare nel pattern P, per ogni x dell'alfabeto del testo T.
voidprintArray(intar[], intdim); // stampa a video un array
intoccurrency(char*testo, char*pattern, intl_testo, intl_pattern); // conteggio occorrenze tramite la matrice m(i,j);
char*substring(char*t1, intin, intf); //copia nell'array temp la sottostringa di lunghezza i dell'array t1
int*shiftRight(int*p, inti); // shifta a destra il puntatore p
int*logicAnd(int*p, intv[], inti); // AND logico
intmain() {
char*testo=(char*)malloc(sizeof(char)*5000000000);
char*pattern=(char*)malloc(sizeof(char)*10);
printf("Inserisci il testo T ");
scanf("%s", testo);
printf("Inserisci il pattern P ");
scanf("%s", pattern);
intlunghTesto=length(testo);
intlunghPattern=length(pattern);
printf("Ci sono %d occorrenze del pattern P nel testo T\n", occurrency(testo, pattern, lunghTesto, lunghPattern));
}
intoccurrency(char*testo, char*pattern, intl_testo, intl_pattern){
/*
DEFINIZIONE DEL CALCOLO DELLA MATRICE M
M(i,j)=1 sse P[: j] = T[i-j+1 : i]
-- La variabile count memorizza il numero di occorrenze del pattern P nel testo T --
Se M(i,j) == 1 quando j==l_pattern --> Incremento di 1 il contatore count
Passo 1: Costruzione dei vettori Ux per ogni x dell'alfabeto sigma
*/
inta[l_pattern]; buildArray(a, pattern, 'a');
intb[l_pattern]; buildArray(b, pattern, 'b');
intc[l_pattern]; buildArray(c, pattern, 'c');
intd[l_pattern]; buildArray(d, pattern, 'd');
inte[l_pattern]; buildArray(e, pattern, 'e');
intf[l_pattern]; buildArray(f, pattern, 'f');
intg[l_pattern]; buildArray(g, pattern, 'g');
inth[l_pattern]; buildArray(h, pattern, 'h');
inti[l_pattern]; buildArray(i, pattern, 'i');
intj[l_pattern]; buildArray(j, pattern, 'j');
intk[l_pattern]; buildArray(k, pattern, 'k');
intl[l_pattern]; buildArray(l, pattern, 'l');
intm[l_pattern]; buildArray(m, pattern, 'm');
intn[l_pattern]; buildArray(n, pattern, 'n');
into[l_pattern]; buildArray(o, pattern, 'o');
intp[l_pattern]; buildArray(p, pattern, 'p');
intq[l_pattern]; buildArray(q, pattern, 'q');
intr[l_pattern]; buildArray(r, pattern, 'r');
ints[l_pattern]; buildArray(s, pattern, 's');
intt[l_pattern]; buildArray(t, pattern, 't');
intu[l_pattern]; buildArray(u, pattern, 'u');
intv[l_pattern]; buildArray(v, pattern, 'v');
intw[l_pattern]; buildArray(w, pattern, 'w');
intx[l_pattern]; buildArray(x, pattern, 'x');
inty[l_pattern]; buildArray(y, pattern, 'y');
intz[l_pattern]; buildArray(z, pattern, 'z');
/* la prima riga della matrice m
viene calcolata esplicitamente
seguendo la definizione
*/
intm1[l_pattern];
intj1;
char*subPattern=substring(pattern, 1, 1); // P[: j]
char*subTesto=substring(testo, 1, 1); // T[i-j+1: i]
if (subPattern[0]==subTesto[0])
m1[0]=1;
else
m1[0]=0;
for(j1=1; j1<l_pattern; j1++){
m1[j1]=0;
}
printf("Riga 1 :\t");
intkk;
for(kk=0; kk<l_pattern; kk++){
printf("%d ", m1[kk]);
}
printf("\n");
intcount=0;
intindex;
int*puntat=m1; // puntatore alla prima riga della matrice
/* Calcolo della righe i della matrice M(i,j):
Per ogni i, M(i,*) viene calcolata come
(R_SHIFT(M(i-1,*))L_AND(U(T[i])))
*/
for(index=1; index<l_testo; index++){
int*m1s=shiftRight(puntat, l_pattern);
int*m2;
switch (testo[index]){
case'a': m2=logicAnd(m1s, a, l_pattern);break;
case'b': m2=logicAnd(m1s, b, l_pattern);break;
case'c': m2=logicAnd(m1s, c, l_pattern);break;
case'd': m2=logicAnd(m1s, d, l_pattern);break;
case'e': m2=logicAnd(m1s, e, l_pattern);break;
case'f': m2=logicAnd(m1s, f, l_pattern);break;
case'g': m2=logicAnd(m1s, g, l_pattern);break;
case'h': m2=logicAnd(m1s, h, l_pattern);break;
case'i': m2=logicAnd(m1s, i, l_pattern);break;
case'j': m2=logicAnd(m1s, j, l_pattern);break;
case'k': m2=logicAnd(m1s, k, l_pattern);break;
case'l': m2=logicAnd(m1s, l, l_pattern);break;
case'm': m2=logicAnd(m1s, m, l_pattern);break;
case'n': m2=logicAnd(m1s, n, l_pattern);break;
case'o': m2=logicAnd(m1s, o, l_pattern);break;
case'p': m2=logicAnd(m1s, p, l_pattern);break;
case'q': m2=logicAnd(m1s, q, l_pattern);break;
case'r': m2=logicAnd(m1s, r, l_pattern);break;
case's': m2=logicAnd(m1s, s, l_pattern);break;
case't': m2=logicAnd(m1s, t, l_pattern);break;
case'u': m2=logicAnd(m1s, u, l_pattern);break;
case'v': m2=logicAnd(m1s, v, l_pattern);break;
case'w': m2=logicAnd(m1s, w, l_pattern);break;
case'x': m2=logicAnd(m1s, x, l_pattern);break;
case'y': m2=logicAnd(m1s, y, l_pattern);break;
case'z': m2=logicAnd(m1s, z, l_pattern);break;
}
puntat=m2;
printf("Riga %d :\t", index+1);
inthh;
for(hh=0; hh<l_pattern; hh++){
printf("%d ", m2[hh]);
}
printf("\n");
if (m2[l_pattern-1]==1)
count++;
}
returncount;
}
int*shiftRight(int*p, inti){
intj;
int*punt=(int*)malloc((i)*sizeof(int));
for(j=1; j<i; j++){
punt[j]=p[j-1];
}
punt[0]=1;
returnpunt;
}
int*logicAnd(int*p, intv[], intj){
int*ret=(int*)malloc((j)*sizeof(int));
inti;
for(i=0; i<j; i++){
if (v[i]==1&&p[i]==1)
ret[i]=1;
else
ret[i]=0;
}
returnret;
}
voidbuildArray(intar[], char*testo, chark){
inti;
for(i=0; i<strlen(testo); i++){
if (testo[i]==k){
ar[i]=1;
}else
ar[i]=0;
}
}
voidprintArray(intar[], intdim){
inti;
for(i=0; i<dim; i++){
printf("%d ", ar[i]);
}
printf("\n");
}
char*substring(char*t1, inti, intf){
char*punt= (char*)malloc((f-i+1)*sizeof(char));
intk=0;
intj;
for(j=i-1; j<f; j++){
punt[k]=t1[j];
k++;
}
returnpunt;
}