forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ea27cc
commit 7b594a4
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include<iostream> | ||
#include<cstring> | ||
using namespace std; | ||
|
||
int editDistance(string s1, string s2){ | ||
int m=s1.length(); | ||
int n=s2.length(); | ||
|
||
int **dp=new int*[m+1]; | ||
for(int i=0;i<=m;i++) | ||
{ | ||
dp[i]=new int[n+1]; | ||
} | ||
for(int i=0;i<=m;i++) | ||
{ | ||
dp[i][0]=i; | ||
} | ||
for(int i=0;i<=n;i++) | ||
{ | ||
dp[0][i]=i; | ||
} | ||
for(int i=1;i<=m;i++) | ||
{ | ||
for(int j=1;j<=n;j++) | ||
{ | ||
if(s1[m-i]==s2[n-j]) | ||
dp[i][j]=dp[i-1][j-1]; | ||
else | ||
{ | ||
dp[i][j]=1+min(dp[i][j-1],min(dp[i-1][j-1],dp[i-1][j])); | ||
} | ||
} | ||
} | ||
return dp[m][n]; | ||
} | ||
|
||
int main(){ | ||
string s1, s2; | ||
cout<<"Enter first string - "; | ||
cin>>s1; | ||
cout<<"Enter second string - "; | ||
cin>>s2; | ||
cout<<"Edit Distance is - "<<editDistance(s1, s2)<<endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include<iostream> | ||
#include <cstring> | ||
#include <climits> | ||
using namespace std; | ||
long int size[1000]; | ||
long int price[1000]; | ||
long long dp[1001][1001]; | ||
int knapsack(int n,int c){ | ||
//base case | ||
if(n==0 || c==0){ | ||
return 0; | ||
} | ||
//look up | ||
if(dp[n][c]!=-1){ | ||
return dp[n][c]; | ||
} | ||
//rec case | ||
int op1,op2; | ||
op1=op2=INT_MIN; | ||
if(c>=size[n-1]){ | ||
op1=price[n-1]+knapsack(n-1,c-size[n-1]); //including the nth item | ||
} | ||
op2=knapsack(n-1,c); //excluding the nth item | ||
dp[n][c]=max(op1,op2); | ||
return dp[n][c]; | ||
} | ||
|
||
int main() { | ||
int n,s; | ||
cin>>n>>s; | ||
for(int i=0;i<n;i++){ | ||
cin>>size[i]; | ||
} | ||
for(int i=0;i<n;i++){ | ||
cin>>price[i]; | ||
} | ||
memset(dp,-1,sizeof dp); | ||
for(int i=0;i<=n;i++){ | ||
dp[i][0]=0; | ||
} | ||
for(int j=0;j<=s;j++){ | ||
dp[0][j]=0; | ||
} | ||
cout<<knapsack(n,s)<<endl; | ||
return 0; | ||
} |