-
Notifications
You must be signed in to change notification settings - Fork 7
/
distancialevenstein.c
50 lines (44 loc) · 1.01 KB
/
distancialevenstein.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
#include <stdio.h>
#include <math.h>
#include <string.h>
#define MIN(x,y) ((x) < (y) ? (x) : (y))
int d[100][100];
void distancia_levenstein(char s[],char t[]);
int main(int argc,char *argv[])
{
if(argc>1&&argc<4)
{
distancia_levenstein(argv[1],argv[2]);
}
else
{
printf("Tiene que insertar 2 palabras ejemplo: ./a.out palabra1 palabra2\n");
}
return 0;
}
void distancia_levenstein(char s[],char t[])
{
int i,j,m,n,temp,tracker;
m = strlen(s);
n = strlen(t);
for(i=0;i<=m;i++)
d[0][i] = i;
for(j=0;j<=n;j++)
d[j][0] = j;
for (j=1;j<=m;j++)
{
for(i=1;i<=n;i++)
{
if(s[i-1] == t[j-1])
{
tracker = 0;
}
else{
tracker = 1;
}
temp = MIN((d[i-1][j]+1),(d[i][j-1]+1));
d[i][j] = MIN(temp,(d[i-1][j-1]+tracker));
}
}
printf("the Levinstein distance is %d\n",d[n][m]);
}