-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlevenstein.c
65 lines (61 loc) · 1.24 KB
/
levenstein.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
int levenstein_distance(const char* a, const char* b, int length_a, int length_b) {
if (!length_a) {
return length_b;
}
if (!length_b) {
return length_a;
}
if (length_b < length_a) {
return levensteinDistance(b, a, length_b, length_a);
}
int prev;
int tmp;
int i1 = 0;
int i2 = 0;
int i3 = 0;
int l1 = length_b + 1;
int f[l1];
s1:
f[i1] = i1;
if (i1++ < l1) {
goto s1;
}
outer:
prev = f[0]++;
inner:
if (f[i3+1] <= f[i3] && b[i3] != a[i2] && f[i3+1] > prev) {
tmp = f[i3+1];
f[i3+1] = prev + 1;
prev = tmp;
} else if (f[i3+1] <= f[i3] && b[i3] != a[i2]) {
prev = f[i3+1]++;
} else if (f[i3+1] <= f[i3] && f[i3+1]+1 > prev) {
tmp = f[i3+1];
f[i3+1] = prev;
prev = tmp;
} else if (f[i3+1] <= f[i3]) {
prev = f[i3+1]++;
} else if (b[i3] != a[i2] && f[i3] > prev) {
tmp = f[i3+1];
f[i3+1] = prev+1;
prev = tmp;
} else if (b[i3] != a[i2]) {
prev = f[i3+1];
f[i3+1] = f[i3] + 1;
} else if (f[i3]+1 > prev) {
tmp = f[i3+1];
f[i3+1] = prev;
prev = tmp;
} else {
prev = f[i3+1];
f[i3+1] = f[i3] + 1;
}
if (++i3 != length_b) {
goto inner;
}
i3 = 0;
if (++i2 != length_a) {
goto outer;
}
return f[length_b];
}