-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimalEditDistance.cpp
More file actions
164 lines (157 loc) · 3.44 KB
/
MinimalEditDistance.cpp
File metadata and controls
164 lines (157 loc) · 3.44 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
/*
BASIC INFO:
stu ID:40843245
name:Huang Jay
Due date:2022/11/25
HW: Algorithm HW3-2
*/
#include <iostream>
#include <string>
using namespace std;
//This code solves Minimal Edit Distance problem
class MinimalEditDistance
{
private:
string srcS;
string tarS;
//Array to store weight (or we can say cost)
//In this code, w[0] is the cost of deletion, 1 for insertion, 2 for replacement.
int w[3];
int wSize;
int **c;
//Array to store action, this array can store the action we need for mininal cost.
int *action;
//The number of action
int step;
int maxAction;
//Temporary array
int tempArr[4];
public:
MinimalEditDistance(string srcS,string tarS,int maxAction,int *w,int wSize)
{
this->srcS=srcS;
this->tarS=tarS;
this->maxAction=maxAction;
this->SetW(w,wSize);
this->step=0;
this->Init();
this->FindMinimalEditDistance();
this->DisplayResult();
}
public:
void Init()
{
this->action=new int[this->maxAction];
for(int i=0;i<this->maxAction;i++)
{
this->action[i]=-1;//set unused.
}
this->c=new int*[this->srcS.length()];
for(int i=0;i<this->srcS.length()+1;i++)
{
this->c[i]=new int[this->tarS.length()+1];
}
}
void SetW(int *w,int wSize)
{
this->wSize=wSize;
for(int i=0;i<this->wSize;i++)
{
this->w[i]=w[i];
}
}
void FindMinimalEditDistance()
{
for(int row=0;row<this->srcS.length()+1;row++)
{
for(int col=0;col<this->tarS.length()+1;col++)
{
if(row==0)
{
this->c[row][col]=col;
}else if(col==0)
{
this->c[row][col]=row;
}else
{
this->CheckOperation(row,col);
}
}
}
}
void CheckOperation(int row,int col)
{
this->tempArr[0]=this->c[row-1][col]+this->w[0];
this->tempArr[1]=this->c[row][col-1]+this->w[1];
this->tempArr[2]=this->c[row-1][col-1]+this->w[2];
this->tempArr[3]=this->c[row-1][col-1];
if(this->srcS[row]==this->tarS[col])
{
this->c[row][col]=this->tempArr[3];
this->action[step]=3;
this->step++;
}else
{
int minIdx=0;
int minVal=this->tempArr[0];
for(int i=0;i<2;i++)
{
if(this->tempArr[i]<minVal)
{
minVal=this->tempArr[i];
minIdx=i;
}
}
this->c[row][col]=minVal;
this->action[step]=minIdx;
this->step++;
}
}
void DisplayResult()
{
this->DisplayC();
//This method is used to show actions.
//Comment the following line if you don't want to see the actions.
//this->DisplayAction();
}
void DisplayC()
{
cout<<this->c[this->srcS.length()][this->tarS.length()]<<endl;
}
void DisplayAction()
{
cout<<"One of sequence of actions to get the minimal edit distance is as follows."<<endl;
for(int i=0;i<this->step;i++)
{
int tempAction=this->action[i];
if(tempAction==0)//Deletion
{
cout<<"Deletion."<<endl;
}
else if(tempAction==1) //Insertion
{
cout<<"Insertion."<<endl;
}else if(tempAction==2)//Replacement
{
cout<<"Replacement."<<endl;
}else if(tempAction==3)//Nothing
{
cout<<"Nothing since the current two char are same."<<endl;
}else //Error occurs.
{
cout<<"ERROR!!!"<<endl;
}
}
}
};
int main()
{
string srcS="";
string tarS="";
int maxLenOfString=1000;
int w[3]={1,1,1};
getline(cin,srcS);
getline(cin,tarS);
MinimalEditDistance *minimalEditDistance=new MinimalEditDistance(srcS,tarS,maxLenOfString,w,sizeof(w)/sizeof(w[0]));
return 0;
}