-
Notifications
You must be signed in to change notification settings - Fork 2
/
FA.c
90 lines (81 loc) · 1.44 KB
/
FA.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define true 1
#define false 0
int min(int a,int b)
{
if(a > b)
return b;
return a;
}
//check whether Pk is suffix of Pq
int suffix(char *pattern,int k,int q,char ch)
{
int i;
if(pattern[k-1] != ch)
return false;
for(i = 0;i < k-1;i++)
if(pattern[i] != pattern[q-k+i+1])
return false;
return true;
}
/*
* construct our FA
*/
void compute_transition_function(char *pattern,int *array,int numchars)
{
int m = strlen(pattern);
int q;
for(q = 0; q <= m; q++)
{
int chars;
for(chars = 0; chars < numchars; chars++)
{
int k = min(m,q+1);
while(k)
{
char ch = 'a'+chars;
if(suffix(pattern,k,q,ch))
break;
k--;
}
array[q*numchars+chars] = k;
}
}
}
int FA_match(char *text,int *array,int numchars,int receive)
{
int n = strlen(text);
int q = 0;
int i;
for(i = 0;i < n;i++)
{
int index = numchars*q+text[i]-97;
q = array[index];
if(q == receive)
return i+1-receive;
}
return -1;
}
int main()
{
char *text = "abababacaba";
char *pattern = "ababaca";
int length = strlen(pattern);
int *array = (int*)malloc(3*(length+1));
compute_transition_function(pattern,array,3);
int i;
printf("This is our chart\n");
for(i = 0;i <= length; i++)
{
int j;
for(j = 0; j < 3; j++)
printf("%d ",array[i*3+j]);
printf("\n");
}
int offset = FA_match(text,array,3,length);
printf("offset is %d\n",offset);
free(array);
return 0;
}