Spoj EDIST - Edit distance Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,i,j;
cin>>t;
while(t--)
{
string a,b;
cin>>a>>b;
int m= a.size();
int n=b.size();
int ans[m+1][n+1];
for(i=0;i<=m;i++)
ans[0][i]=i;
for(j=0;j<=n;j++)
ans[j][0]=j;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(a[i-1]==b[j-1])
ans[i][j]=ans[i-1][j-1];
else
ans[i][j]=1+min(ans[i-1][j-1],min(ans[i-1][j],ans[i][j-1]));
}
}
cout<<ans[m][n]<<"\n";
}
return 0;
}
using namespace std;
int main()
{
int t,i,j;
cin>>t;
while(t--)
{
string a,b;
cin>>a>>b;
int m= a.size();
int n=b.size();
int ans[m+1][n+1];
for(i=0;i<=m;i++)
ans[0][i]=i;
for(j=0;j<=n;j++)
ans[j][0]=j;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(a[i-1]==b[j-1])
ans[i][j]=ans[i-1][j-1];
else
ans[i][j]=1+min(ans[i-1][j-1],min(ans[i-1][j],ans[i][j-1]));
}
}
cout<<ans[m][n]<<"\n";
}
return 0;
}
Comments
Post a Comment