Skip to content

Commit a9fb48b

Browse files
committed
Edit DIstance Added
1 parent 111d880 commit a9fb48b

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
2+
/**
3+
4+
Problem : Levenshtein /Edit Distance
5+
The Levenshtein distance between two words is the minimum number of single-character edits
6+
(i.e. insertions, deletions or substitutions) required to change one word into the other.
7+
Each of these operations has unit cost.
8+
9+
**/
10+
11+
/**Which of the favors of your Lord will you deny ?**/
12+
13+
#include<bits/stdc++.h>
14+
using namespace std;
15+
16+
#define LL long long
17+
#define PII pair<int,int>
18+
#define PLL pair<LL,LL>
19+
#define MP make_pair
20+
#define F first
21+
#define S second
22+
#define INF INT_MAX
23+
24+
#define ALL(x) (x).begin(), (x).end()
25+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
26+
27+
#include <ext/pb_ds/assoc_container.hpp>
28+
#include <ext/pb_ds/tree_policy.hpp>
29+
using namespace __gnu_pbds;
30+
31+
template<class TIn>
32+
using indexed_set = tree<
33+
TIn, null_type, less<TIn>,
34+
rb_tree_tag, tree_order_statistics_node_update>;
35+
36+
/*
37+
PBDS
38+
-------------------------------------------------
39+
1) insert(value)
40+
2) erase(value)
41+
3) order_of_key(value) // 0 based indexing
42+
4) *find_by_order(position) // 0 based indexing
43+
44+
*/
45+
46+
inline void optimizeIO()
47+
{
48+
ios_base::sync_with_stdio(false);
49+
cin.tie(NULL);
50+
}
51+
52+
const int nmax = 2e5+7;
53+
const LL LINF = 1e17;
54+
55+
string to_str(LL x)
56+
{
57+
stringstream ss;
58+
ss<<x;
59+
return ss.str();
60+
}
61+
62+
//bool cmp(const PII &A,const PII &B)
63+
//{
64+
//
65+
//}
66+
67+
unordered_map<string,int>dp;
68+
69+
int e_length(string X,string Y,int m,int n)
70+
{
71+
if(m==0 || n==0)
72+
return max(m,n); /** the one which is not 0 **/
73+
74+
string key = to_str(m) + "|" + to_str(n);
75+
76+
if(dp.find(key)!=dp.end())
77+
return dp[key];
78+
79+
if(X[m-1]==Y[n-1])
80+
dp[key] = e_length(X,Y,m-1,n-1);
81+
else
82+
dp[key] = 1 + min(e_length(X,Y,m-1,n-1),min(e_length(X,Y,m,n-1),e_length(X,Y,m-1,n)));
83+
84+
return dp[key];
85+
}
86+
87+
string e_print(string X,string Y,int m,int n)
88+
{
89+
if(m==0 && n==0) return "";
90+
if(m==0) return "+("+Y.substr(0,n)+")"; /** Add the part of Y which is left **/
91+
if(n==0) return "-("+X.substr(0,m)+")"; /** Delete the part of X which is left **/
92+
93+
if(X[m-1]==Y[n-1])
94+
return e_print(X,Y,m-1,n-1) + X.substr(m-1,1);
95+
96+
string key1 = to_str(m-1) + "|" + to_str(n-1);
97+
string key2 = to_str(m-1) + "|" + to_str(n);
98+
string key3 = to_str(m) + "|" + to_str(n-1);
99+
100+
int val1 = dp[key1] , val2 = dp[key2] , val3 = dp[key3];
101+
int minn = min(val1,min(val2,val3));
102+
103+
if(minn==val1) return e_print(X,Y,m-1,n-1) + "(-"+X.substr(m-1,1)+"+"+Y.substr(n-1,1)+")"; /** Substitution **/
104+
if(minn==val2) return e_print(X,Y,m-1,n) + "-"+X.substr(m-1,1); /** Deletion **/
105+
if(minn==val3) return e_print(X,Y,m,n-1) + "+"+Y.substr(n-1,1); /** Addition **/
106+
107+
}
108+
109+
int main()
110+
{
111+
optimizeIO();
112+
113+
string X = "kitten";
114+
string Y = "sitting";
115+
116+
// string X = "kiy";
117+
// string Y = "sitting";
118+
119+
cout<<"X : "<<X<<endl;
120+
cout<<"Y : "<<Y<<endl;
121+
122+
cout<<"Edit Distance : "<<e_length(X,Y,X.size(),Y.size())<<endl;
123+
cout<<"Edit String : "<<e_print(X,Y,X.size(),Y.size())<<endl;
124+
125+
return 0;
126+
}
127+
128+

0 commit comments

Comments
 (0)