-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditDistance.cpp
91 lines (88 loc) · 1.79 KB
/
EditDistance.cpp
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
#include <stdio.h>
#include <string.h>
const int strLen = 25;
int sol[strLen][strLen];
int next [strLen][strLen];
char* one,*two;
int n,m;
int numDeleted;
int solve()
{
n = strlen(one);
m = strlen(two);
for(int i = m; i>=0; i--){
sol[n][i]=m-i;
}
for(int i = n-1; i>=0; i--){
sol[i][m]=n-i;
}
for(int o = n-1; o>=0; o--){
for(int t = m-1; t>=0; t--){
sol[o][t] = one[o]==two[t]?sol[o+1][t+1]:sol[o+1][t+1]+1;
sol[o][t] = sol[o][t]< sol[o][t+1]+1? sol[o][t] : sol[o][t+1]+1;
sol[o][t] = sol[o][t]< sol[o+1][t]+1? sol[o][t] : sol[o+1][t]+1;
next[o][t]= sol[o][t]==sol[o][t+1]+1 ? 1
:(sol[o][t]==sol[o+1][t]+1?2:0);
next[o][t]= next[o][t]==0 && one[o]==two[t]?-1:next[o][t];
}
}
return sol[0][0];
}
//Prints out how to transform one string to the other.
void print(int o,int t){
if(o==n && t==m){
printf("E\n");//END
}
else if (o==n){
for(int i = t; i <m; i++){
printf("I%c%02d",two[i],(o+i-t-numDeleted+1));//INSERT
}
printf("E\n");//END
}
else if (t==m){
for(int i = o; i <n; i++)
printf("D%c%02d",one[i],(o-numDeleted+1));//DELETE
printf("E\n");
}
else {
switch (next[o][t]){
case 0:
printf("C%c%02d",two[t],(o-numDeleted+1));//CHANGE
print(o+1,t+1);
break;
case -1:
print(o+1,t+1);//ALL GOOD
break;
case 1:
printf("I%c%02d",two[t],(o-numDeleted+1));//INSERT
numDeleted--;
print(o,t+1);
break;
case 2:
printf("D%c%02d",one[o],(o-numDeleted+1));//DELETE
numDeleted++;
print(o+1,t);
break;
default:
printf("E\n");//END
break;
}
}
}
void print(){
numDeleted = 0;
print(0,0);
}
int main(){
while(true){
one = new char[strLen];
two = new char[strLen];
scanf("%s",one);
if(one[0]=='#')
break;
scanf("%s",two);
printf("num changed %d\n",solve());
print();
}
return 0;
}