diff --git a/Dynamic Programming/DP over Subsets/subsetpartition.cpp b/Dynamic Programming/DP over Subsets/subsetpartition.cpp new file mode 100644 index 0000000..7d95201 --- /dev/null +++ b/Dynamic Programming/DP over Subsets/subsetpartition.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +#define int int64_t +#define vi vector +#define vb vector +#define vbb vector< vector > +#define all(x) (x).begin(), (x).end() +#define vii vector< vector > +#define ff first +#define ss second +#define pii pair +#define mp make_pair +#define pb push_back +#define f(i,n) for(i=0;i +#define pqs priority_queue > +#define setbits(x) __builtin_popcountll(x) +#define zrobits(x) __builtin_ctzll(x) +#define mod 1000000007 +#define inf 1e18 +#define ps(x,y) fixed<>x; while(x--) +#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + + +int32_t main() +{ + int i, n, j, k, l, t; + cin >> n; + vi v(n); + f(i,n) cin >> v[i]; + l=0; + f(i,n) l += v[i]; + + // cout << l; + if(l&1) + { + cout << -1; + return 0; + } + l/=2; + + vbb dp(n+1, vb(l+1,false)); + f(i,n+1) dp[i][0] = true; + for(i=1;i<=n;i++) + { + for(j=1;j<=l;j++) + { + // if(i==0) dp[i][] + if(j >= v[i-1]) dp[i][j] = dp[i-1][j-v[i-1]] + dp[i-1][j]; + else dp[i][j] = dp[i-1][j]; + } + } + + if(dp[n][l]) cout << "Possible"; + else cout << "Not Possible"; +} \ No newline at end of file diff --git a/Dynamic Programming/Recursion and DAC/Printlcs.cpp b/Dynamic Programming/Recursion and DAC/Printlcs.cpp new file mode 100644 index 0000000..c35a810 --- /dev/null +++ b/Dynamic Programming/Recursion and DAC/Printlcs.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +#define int int64_t +#define vi vector +#define vb vector +#define vbb vector< vector > +#define all(x) (x).begin(), (x).end() +#define vii vector< vector > +#define ff first +#define ss second +#define pii pair +#define mp make_pair +#define pb push_back +#define f(i,n) for(i=0;i +#define pqs priority_queue > +#define setbits(x) __builtin_popcountll(x) +#define zrobits(x) __builtin_ctzll(x) +#define mod 1000000007 +#define inf 1e18 +#define ps(x,y) fixed<>x; while(x--) +#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int32_t main() +{ + FIO; + int i, m, n, j, k, l, t; + string s1, s2; + cin >> s1 >> s2; + n = s1.length(); + m = s2.length(); + vii dp(n+1, vi(m+1,0)); + for(i=1;i<=n;i++) + { + for(j=1;j<=m;j++) + { + if(s1[i-1]==s2[j-1]) + dp[i][j] = dp[i-1][j-1]+1; + else + dp[i][j] = max(dp[i-1][j], dp[i][j-1]); + } + } + + f(i,n+1) + { + f(j,m+1) cout << dp[i][j] << " "; + cout << endl; + } + i=n, j=m; + stack s; + + while(i and j) + { + if(s1[i-1]==s2[j-1]) + { + s.push(s1[i-1]); + i--,j--; + } + else if(dp[i-1][j] > dp[i][j-1]) i--; + else j--; + } + + while(!s.empty()) + { + cout << s.top(); + s.pop(); + } +} \ No newline at end of file diff --git a/Dynamic Programming/Recursion and DAC/knapsackDown.cpp b/Dynamic Programming/Recursion and DAC/knapsackDown.cpp new file mode 100644 index 0000000..206570b --- /dev/null +++ b/Dynamic Programming/Recursion and DAC/knapsackDown.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +#define int int64_t +#define vi vector +#define all(x) (x).begin(), (x).end() +#define vii vector< vector > +#define ff first +#define ss second +#define pii pair +#define mp make_pair +#define pb push_back +#define f(i,n) for(i=0;i +#define pqs priority_queue > +#define setbits(x) __builtin_popcountll(x) +#define zrobits(x) __builtin_ctzll(x) +#define mod 1000000007 +#define inf 1e18 +#define ps(x,y) fixed<>x; while(x--) +#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int32_t main() +{ + int n, W, i, j; cin >> n >> W; + vi val(n), wt(n); + f(i,n) cin >> val[i]; + f(i,n) cin >> wt[i]; + +// int i, j; + vector dp(W+1,0), cur_dp(W+1,0); + + + bool k=true; + for(i=1;i=0;j--) + { + if(k) + { + if(wt[i-1] +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +#define int int64_t +#define vi vector +#define all(x) (x).begin(), (x).end() +#define vii vector< vector > +#define ff first +#define ss second +#define pii pair +#define mp make_pair +#define pb push_back +#define f(i,n) for(i=0;i +#define pqs priority_queue > +#define setbits(x) __builtin_popcountll(x) +#define zrobits(x) __builtin_ctzll(x) +#define mod 1000000007 +#define inf 1e18 +#define ps(x,y) fixed<>x; while(x--) +#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +// int dp[1000][1000]={-1}; +vii dp(1000,vi(1000, -1)); +int ans(const vector v, int n, int w) +{ + if(n==0 or w==0) return 0; + if(dp[n][w]!=-1) return dp[n][w]; + else + { + if(n >= v[n-1].ff) + dp[n][w] = max(ans(v,n-1,w), ans(v, n-1, w - v[n-1].ff)+v[n-1].ss); + + else + dp[n][w] = ans(v,n-1,w); + + return dp[n][w]; + } + +} + + +int32_t main() +{ + int i, n, j, w, k, l, t; + cin >> n >> w; + vector v(n); + f(i,n) cin >> v[i].ff >> v[i].ss ; + cout << ans(v, n, w); + +} \ No newline at end of file