Skip to content

Instantly share code, notes, and snippets.

@mangalvikas
Created November 6, 2016 13:23
Show Gist options
  • Select an option

  • Save mangalvikas/90c844b4ac0465fd3effb180f692739e to your computer and use it in GitHub Desktop.

Select an option

Save mangalvikas/90c844b4ac0465fd3effb180f692739e to your computer and use it in GitHub Desktop.
This is a solution of Dynamic programming problem
#include<bits/stdc++.h>
using namespace std;
int min_dis(string s1,string s2){
int l1=s1.length();
int l2=s2.length();
int dp[l2+1][l1+1];
int i,j;
for(i=0;i<l1+1;i++){
dp[0][i]=i;
}
for(i=0;i<l2+1;i++){
dp[i][0]=i;
}
for(i=1;i<l2+1;i++){
for(j=1;j<l1+1;j++){
if(s2[i-1]==s1[j-1]){
dp[i][j]=dp[i-1][j-1];
}else{
dp[i][j]=1+min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]));
}
}
}
return dp[l2][l1];
}
int main(){
int t;
cin>>t;
string s1,s2;
while(t--){
cin>>s1>>s2;
cout<<min_dis(s1,s2)<<"\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment