#include <iostream>
#include <string.h>
using namespace std;
int DamerauLevenshtein(char[], char[], int, int);
int main()
{
char A[200000], B[200000];
cout<< "Input 1: ";
cin>>A;
cout<<"Input 2: ";
cin>>B;
cout<<"Damerau Levenshtein distance: "<<DamerauLevenshtein(A,B,strlen(A),strlen(B));
return 0;
}
int DamerauLevenshtein(char A[200000], char B[200000], int m, int n)
{
int table[m+1][n+1];
int cost;
int counter=0;
for(int i=0; i<=m; i++)
{
for(int j=0; j<=n; j++)
{
table[i][j]=0;
}
}
for(int i=0; i<=m; i++)
{
table[i][0]= i;
}
for(int j=0; j<=n; j++)
{
table[0][j]= j;
}
for(int j=1; j<=n; j++)
{
for(int i=1; i<=m; i++)
{
counter++;
if(A[i-1]==B[j-1])
{
cost=0;
}
else
{
cost=1;
}
int minimum, deletion, substitution, insertion;
deletion= table[i-1][j]+1;
insertion= table[i][j-1]+1;
substitution= table[i-1][j-1]+cost;
minimum= deletion;
if(insertion<minimum)
{
minimum= insertion;
}
else if(substitution<minimum)
{
minimum= substitution;
}
table[i][j]= minimum;
if((i>1 and j>1))
{
if((A[i-1]==B[j-2]) and (A[i-2]==B[j-1]))
{
int transposition= table[i-2][j-2]+cost;
if(table[i][j]>transposition)
{
table[i][j]= transposition;
}
}
}
}
}
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
cout<<table[i][j];
}
cout<<endl;
}
cout<<"Number of iteration: "<<counter<<endl;
return table[m][n];
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IERhbWVyYXVMZXZlbnNodGVpbihjaGFyW10sIGNoYXJbXSwgaW50LCBpbnQpOwoKaW50IG1haW4oKQp7CiAgICBjaGFyIEFbMjAwMDAwXSwgQlsyMDAwMDBdOwogICAgY291dDw8ICJJbnB1dCAxOiAiOwogICAgY2luPj5BOwogICAgY291dDw8IklucHV0IDI6ICI7CiAgICBjaW4+PkI7CiAgICAKICAgIGNvdXQ8PCJEYW1lcmF1IExldmVuc2h0ZWluIGRpc3RhbmNlOiAiPDxEYW1lcmF1TGV2ZW5zaHRlaW4oQSxCLHN0cmxlbihBKSxzdHJsZW4oQikpOwogICAgcmV0dXJuIDA7Cn0KCgppbnQgRGFtZXJhdUxldmVuc2h0ZWluKGNoYXIgQVsyMDAwMDBdLCBjaGFyIEJbMjAwMDAwXSwgaW50IG0sIGludCBuKQp7CiAgICBpbnQgdGFibGVbbSsxXVtuKzFdOwogICAgCiAgICBpbnQgY29zdDsKICAgIGludCBjb3VudGVyPTA7CiAgICAKICAgIGZvcihpbnQgaT0wOyBpPD1tOyBpKyspCiAgICB7CiAgICAJZm9yKGludCBqPTA7IGo8PW47IGorKykKICAgIAl7CiAgICAJCXRhYmxlW2ldW2pdPTA7CiAgICAJfQogICAgfQogICAgZm9yKGludCBpPTA7IGk8PW07IGkrKykKICAgIHsKICAgICAgICB0YWJsZVtpXVswXT0gaTsKICAgIH0KICAgIGZvcihpbnQgaj0wOyBqPD1uOyBqKyspCiAgICB7CiAgICAgICAgdGFibGVbMF1bal09IGo7CiAgICB9CiAgIAogICAgZm9yKGludCBqPTE7IGo8PW47IGorKykKICAgIHsKICAgICAgICBmb3IoaW50IGk9MTsgaTw9bTsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgY291bnRlcisrOwogICAgICAgICAgICAKICAgICAgICAgICAgaWYoQVtpLTFdPT1CW2otMV0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvc3Q9MDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvc3Q9MTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgaW50IG1pbmltdW0sIGRlbGV0aW9uLCBzdWJzdGl0dXRpb24sIGluc2VydGlvbjsKICAgICAgICAgICAgCiAgICAgICAgICAgIGRlbGV0aW9uPSB0YWJsZVtpLTFdW2pdKzE7CiAgICAgICAgICAgIGluc2VydGlvbj0gdGFibGVbaV1bai0xXSsxOwogICAgICAgICAgICBzdWJzdGl0dXRpb249IHRhYmxlW2ktMV1bai0xXStjb3N0OwogICAgICAgICAgICAKICAgICAgICAgICAgbWluaW11bT0gZGVsZXRpb247CiAgICAgICAgICAgIGlmKGluc2VydGlvbjxtaW5pbXVtKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBtaW5pbXVtPSBpbnNlcnRpb247CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZihzdWJzdGl0dXRpb248bWluaW11bSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbWluaW11bT0gc3Vic3RpdHV0aW9uOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICB0YWJsZVtpXVtqXT0gbWluaW11bTsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKChpPjEgYW5kIGo+MSkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgCWlmKChBW2ktMV09PUJbai0yXSkgYW5kIChBW2ktMl09PUJbai0xXSkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgaW50IHRyYW5zcG9zaXRpb249IHRhYmxlW2ktMl1bai0yXStjb3N0OwogICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICBpZih0YWJsZVtpXVtqXT50cmFuc3Bvc2l0aW9uKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHRhYmxlW2ldW2pdPSB0cmFuc3Bvc2l0aW9uOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICAgICAgZm9yIChpbnQgaT0wOyBpPD1tOyBpKyspCiAgICAgICAgewogICAgICAgIAlmb3IgKGludCBqPTA7IGo8PW47IGorKykKICAgICAgICAgICAgIHsKICAgICAgICAgICAgIAljb3V0PDx0YWJsZVtpXVtqXTsKICAgICAgICAgICAgIH0KICAgICAgICAgICAgIGNvdXQ8PGVuZGw7CiAgICAgICAgfQogICAgCiAgICAKICAgIGNvdXQ8PCJOdW1iZXIgb2YgaXRlcmF0aW9uOiAiPDxjb3VudGVyPDxlbmRsOwogICAgCiAgICByZXR1cm4gdGFibGVbbV1bbl07CiAgICAKfQo=