Edit Distance
PROBLEM :
Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required to convert ‘str1' into ‘str2'.
Insert
Remove
Replace
All of the above operations are of cost=1.
Both the strings are of lowercase.
Input:
The First line of the input contains an integer T denoting the number of test cases. Then T test cases follow. Each tese case consists of two lines. The first line of each test case consists of two space separated integers P and Q denoting the length of the strings str1 and str2 respectively. The second line of each test case coantains two space separated strings str1 and str2 in order.
Output:
Coreesponding to each test case, pirnt in a new line, the minimum number of operations required.
Constraints:
1<=T<=30
1<= Length(str1) <= 100
1<= Length(str2) <= 100
Example:
Input:
1
4 5
geek gesek
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Edit_Distance(char[],char[],int,int) ;
int min(int ,int ,int ) ;
int main()
{
int t,n1,n2,i,ans ;
char str1[100],str2[100] ;
cin>>t ;
while(t--)
{
cin>>n1>>n2 ;
for(i=0;i<n1;i++)
cin>>str1[i] ;
for(i=0;i<n2;i++)
cin>>str2[i] ;
ans=Edit_Distance(str1,str2,n1,n2) ;
cout<<ans ;
cout<<endl ;
}
return 0;
}
int Edit_Distance(char str1[],char str2[],int no1,int no2)
{
int mat[no1+1][no2+1] ;
int i,j ;
for(i=0;i<=no1;i++)
{
for(j=0;j<=no2;j++)
{
if(i==0)
mat[i][j]=j ;
if(j==0)
mat[i][j]=i ;
}
}
for(i=1;i<=no1;i++)
{
for(j=1;j<=no2;j++)
{
if(str1[i-1]==str2[j-1])
mat[i][j]=mat[i-1][j-1] ;
else
mat[i][j]=1+min(mat[i-1][j-1],mat[i][j-1],mat[i-1][j]) ;
}
}
return mat[no1][no2] ;
}
int min(int a,int b,int c)
{
int temp,min ;
temp = (a<b)?a:b;
min=(c<temp)?c:temp;
return min ;
}
---------------------------------------------------------------------------------
Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required to convert ‘str1' into ‘str2'.
Insert
Remove
Replace
All of the above operations are of cost=1.
Both the strings are of lowercase.
Input:
The First line of the input contains an integer T denoting the number of test cases. Then T test cases follow. Each tese case consists of two lines. The first line of each test case consists of two space separated integers P and Q denoting the length of the strings str1 and str2 respectively. The second line of each test case coantains two space separated strings str1 and str2 in order.
Output:
Coreesponding to each test case, pirnt in a new line, the minimum number of operations required.
Constraints:
1<=T<=30
1<= Length(str1) <= 100
1<= Length(str2) <= 100
Example:
Input:
1
4 5
geek gesek
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Edit_Distance(char[],char[],int,int) ;
int min(int ,int ,int ) ;
int main()
{
int t,n1,n2,i,ans ;
char str1[100],str2[100] ;
cin>>t ;
while(t--)
{
cin>>n1>>n2 ;
for(i=0;i<n1;i++)
cin>>str1[i] ;
for(i=0;i<n2;i++)
cin>>str2[i] ;
ans=Edit_Distance(str1,str2,n1,n2) ;
cout<<ans ;
cout<<endl ;
}
return 0;
}
int Edit_Distance(char str1[],char str2[],int no1,int no2)
{
int mat[no1+1][no2+1] ;
int i,j ;
for(i=0;i<=no1;i++)
{
for(j=0;j<=no2;j++)
{
if(i==0)
mat[i][j]=j ;
if(j==0)
mat[i][j]=i ;
}
}
for(i=1;i<=no1;i++)
{
for(j=1;j<=no2;j++)
{
if(str1[i-1]==str2[j-1])
mat[i][j]=mat[i-1][j-1] ;
else
mat[i][j]=1+min(mat[i-1][j-1],mat[i][j-1],mat[i-1][j]) ;
}
}
return mat[no1][no2] ;
}
int min(int a,int b,int c)
{
int temp,min ;
temp = (a<b)?a:b;
min=(c<temp)?c:temp;
return min ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment