-
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
Showing
7 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,22 @@ | ||
#include <stdio.h> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int max(int a, int b){return a>b?a:b;} | ||
|
||
int ks(vector<int> &val, vector<int> &wt, int maxW){ | ||
vector<int> dp(maxW+1,0); | ||
for(int i=0;i<val.size();i++) for(int j=maxW;j>=wt[i];j--) dp[j]=max(dp[j],dp[j-wt[i]]+val[i]); | ||
return dp[maxW]; | ||
} | ||
|
||
int main() { | ||
vector<int> val = {60, 100, 120}; | ||
vector<int> wt = {10, 20, 30}; | ||
int maxW = 50; | ||
|
||
printf("%d\n",ks(val,wt,maxW)); | ||
|
||
return 0; | ||
} |
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,29 @@ | ||
#include <stdio.h> | ||
#include <vector> | ||
#include <limits.h> | ||
|
||
using namespace std; | ||
|
||
int min(int a, int b){return a<b?a:b;} | ||
|
||
int coin(vector<int> &v, int target){ | ||
vector<int> dp(target+1,INT_MAX-1); | ||
for(int ii=1;ii<=target;ii++) printf("%d ",dp[ii]); | ||
printf("\n"); | ||
dp[0]=0; | ||
for(int i=0;i<v.size();i++){ | ||
for(int j=target;j>=v[i];j--){ | ||
dp[j]=min(dp[j],dp[j-v[i]]+1); | ||
} | ||
for(int ii=1;ii<=target;ii++) printf("%d ",dp[ii]); | ||
printf("\n"); | ||
} | ||
return dp[target]; | ||
} | ||
|
||
int main(){ | ||
vector<int> v={1,5,6,8}; | ||
int target= 11; | ||
printf("%d\n",coin(v,target)); | ||
return 0; | ||
} |
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,21 @@ | ||
#include <stdio.h> | ||
#include <vector> | ||
#include <limits.h> | ||
|
||
using namespace std; | ||
|
||
int min(int a, int b){return a<b?a:b;} | ||
|
||
int coin(vector<int> &v, int target){ | ||
vector<int> dp(target+1,INT_MAX-1); | ||
dp[0]=0; | ||
for(int i=0;i<v.size();i++) for(int j=v[i];j<=target;j++) dp[j]=min(dp[j],dp[j-v[i]]+1); | ||
return dp[target]; | ||
} | ||
|
||
int main(){ | ||
vector<int> v={1,5,6,8}; | ||
int target= 11; | ||
printf("%d\n",coin(v,target)); | ||
return 0; | ||
} |
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,21 @@ | ||
#include <stdio.h> | ||
#include <string> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int lcs(string a, string b){ | ||
vector<vector<int>> dp(a.length()+1,vector<int>(b.length()+1,0)); | ||
for(int i=1;i<=a.length();i++){ | ||
for(int j=1;j<=b.length();j++){ | ||
if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; | ||
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); | ||
} | ||
} | ||
return dp[a.length()][b.length()]; | ||
} | ||
|
||
int main(){ | ||
printf("%d\n",lcs("abcdaf","acbcf")); | ||
return 0; | ||
} |
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,31 @@ | ||
#include <stdio.h> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
bool subset(vector<int> &v, int target){ | ||
vector<vector<bool> > dp(v.size()+1,vector<bool>(target+1,1)); | ||
for(int i=0;i<=v.size();i++){ | ||
for(int j=0;j<=target;j++){ | ||
if(j==0) dp[i][j]=1; | ||
else if(i==0) dp[i][j]=0; | ||
else if(j-v[i-1]>=0) dp[i][j]=dp[i-1][j]||dp[i-1][j-v[i-1]]; | ||
else dp[i][j]=dp[i-1][j]; | ||
} | ||
} | ||
|
||
for(int i=1;i<=v.size();i++){ | ||
for(int j=1;j<=target;j++){ | ||
printf("%3d",dp[i][j]? 1:0); | ||
} | ||
printf("\n"); | ||
} | ||
return dp[v.size()][target]; | ||
} | ||
|
||
int main(){ | ||
vector<int> v = {2,3,7,8,10}; | ||
int target = 32; | ||
printf("%d\n",subset(v,target)? 1:0); | ||
return 0; | ||
} |