-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdp-22(editing distance).cpp
66 lines (63 loc) · 1.48 KB
/
dp-22(editing distance).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
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
vector<char>aa,bb;
int ara[101][101];
string a,b;
int editing_distance(string a,string b,int row,int col)
{
for(int i=0;i<=row;i++)ara[i][0]=i;
for(int i=0;i<=col;i++)ara[0][i]=i;
for(int i=1;i<=row;i++){
for(int j=1;j<=col;j++){
if(a[i-1]==b[j-1]){
ara[i][j]=min(ara[i][j-1]+1,min(ara[i-1][j]+1,ara[i-1][j-1]));
}
else{
ara[i][j]=min(ara[i][j-1]+1,min(ara[i-1][j]+1,ara[i-1][j-1]+1));
}
}
}
return ara[row][col];
}
/* using backtracking to find the allignment*/
void allignment(int i,int j)
{
if(i==0 and j==0)return;
if(i>0 and ara[i][j]==(ara[i-1][j]+1))
{
allignment(i-1,j);
aa.push_back(a[i-1]);
bb.push_back('-');
}
else if(j>0 and ara[i][j]==(ara[i][j-1]+1))
{
allignment(i,j-1);
aa.push_back('-');
bb.push_back(b[j-1]);
}
else
{
allignment(i-1,j-1);
aa.push_back(a[i-1]);
bb.push_back(b[j-1]);
}
}
int main()
{
int row,col;
cin>>a>>b;
row=a.size();
col=b.size();
cout<<editing_distance(a,b,row,col)<<endl;
allignment(row,col);
for(int i=0;i<aa.size();i++){
cout<<aa[i];
}
cout<<endl;
for(int i=0;i<bb.size();i++){
cout<<bb[i];
}
cout<<endl;
return 0;
}