// // 5th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // Basic LCS problem using dynamic programming // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/31. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
char s1[1010],s2[1010]; int dp[1010][1010];
intLCS(int len_s1,int len_s2) { for (int i=0;i<=len_s1;i++) dp[i][0]=0; for (int i=0;i<=len_s2;i++) dp[0][i]=0; for (int i=1;i<=len_s1;i++) for (int j=1;j<=len_s2;j++) { if (s1[i]==s2[j]) { dp[i][j] = dp[i-1][j-1]+1; continue; } dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } return dp[len_s1][len_s2]; } voidgener_LCS(int tail1, int tail2) { if (tail1==0 || tail2==0) return; if (s1[tail1]==s2[tail2]) { gener_LCS(tail1-1, tail2-1); cout << s1[tail1]; } elseif (dp[tail1-1][tail2]>=dp[tail1][tail2-1]) gener_LCS(tail1-1, tail2); else gener_LCS(tail1, tail2-1); return; } intmain() { cin >> s1+1; cin >> s2+1; int n1 = strlen(s1+1); int n2 = strlen(s2+1); cout << LCS(n1, n2) << endl; gener_LCS(n1, n2); cout << endl; }
// // 5th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // modified from the LCS problem, here i change the meaning of dp[i][j]. // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/31. // // #include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x));
char s1[1010],s2[1010]; int dp[1010][1010]; int ansx,ansy; int ret;
voidLCS(int len_s1,int len_s2) { for (int i=0;i<=len_s1;i++) dp[i][0]=0; for (int i=0;i<=len_s2;i++) dp[0][i]=0; for (int i=1;i<=len_s1;i++) for (int j=1;j<=len_s2;j++) { if (s1[i]==s2[j]) { dp[i][j] = dp[i-1][j-1]+1; if (dp[i][j]>ret) { ret = dp[i][j]; ansx = i; ansy = j; } continue; } dp[i][j] = 0; } } voidgener_LCS() { if (ret==0) { cout << "Failed to find" <<endl; return; } int headx,heady; for (int i=ansx,j=ansy;;i--,j--) { if (i==0 || j==0 || s1[i]!=s2[j]) { headx = i+1; heady = j+1; break; } } for (int i=headx,j=heady;i<=ansx;i++,j++) cout << s1[i]; cout << endl; } intmain() { cin >> s1+1; cin >> s2+1; int n1 = strlen(s1+1); int n2 = strlen(s2+1); ret =0; LCS(n1, n2); cout << ret << endl; gener_LCS(); }
// // 5th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/31. // //
#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); string s1,s2; int dp[1010][1010]; set<string> ans;
intLCS(int len_s1,int len_s2) { for (int i=0;i<=len_s1;i++) dp[i][0]=0; for (int i=0;i<=len_s2;i++) dp[0][i]=0; for (int i=1;i<=len_s1;i++) for (int j=1;j<=len_s2;j++) { if (s1[i]==s2[j]) { dp[i][j] = dp[i-1][j-1]+1; continue; } dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } return dp[len_s1][len_s2]; } voidgener_LCS(int tail1, int tail2, string xx) { if (dp[tail1][tail2]==0) { ans.insert(xx); return; } if (s1[tail1]==s2[tail2]) { string temp; temp = s1[tail1]; xx.insert(0, temp); gener_LCS(tail1-1, tail2-1,xx); } elseif (dp[tail1-1][tail2]>dp[tail1][tail2-1]) gener_LCS(tail1-1, tail2,xx); elseif (dp[tail1-1][tail2]<dp[tail1][tail2-1]) gener_LCS(tail1, tail2-1,xx); else { gener_LCS(tail1-1, tail2,xx); gener_LCS(tail1, tail2-1,xx); } return; }
// // 5th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/31. // //
#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); int mat[105][105]; int sub[105]; int s[105]; int m,n;
intsubmax() { int ret = s[1]; sub[1] = s[1]; for (int i=2;i<=n;i++) { if (sub[i-1]<0) sub[i] = s[i]; else sub[i] = s[i]+sub[i-1]; ret = max(ret,sub[i]); } return ret; }
intsum_matrix() { int ans=-INF; for (int i=1;i<=m;i++) { fulset(s, 0); for (int j=i;j<=m;j++) { for(int k=1;k<=n;k++) s[k]+=mat[j][k]; ans = max(ans,submax()); } } return ans; }
intmain() { cin >> m >> n; for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) cin >> mat[i][j]; cout << sum_matrix() << endl; }
// // 5th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/3/31. // //
#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x)); int a[501]; int dp[501][501]; //dp[i][j] : formal j items divided into i subsets
intmax_sum(int n,int m) { for (int i=0;i<=n;i++) dp[0][i] = 0; for (int i=0;i<=m;i++) dp[i][0] = 0; for (int i=1;i<=m;i++) { dp[i][i] = dp[i-1][i-1] + a[i]; int submax = dp[i-1][i-1]; for (int j=i+1;j<=n;j++) { submax = max(submax,dp[i-1][j-1]); dp[i][j] = max(submax,dp[i][j-1]) + a[j]; } } int ret = -INF; for (int i=m;i<=n;i++) ret = max(ret,dp[m][i]); return ret; }
intmain() { int n,m; cin >> n >> m; //m subsets for (int i=1;i<=n;i++) cin >> a[i]; cout << max_sum(n, m) << endl; }
// // 8th homework of Algorithm Designing and Analyzing in spring semester, 2020 // // Introduction: // // // Author: // Yihang Bao // 2018011890 // Created by Yihang Bao on 2020/4/15. // //
#include<bits/stdc++.h> #define INF 0x3f3f3f3 #define maxn 100100 typedeflonglong ll; usingnamespacestd; #define rep(i, n, args...) for (ll i = 0, ##args; i < n; i++) #define repr(i, s, e, args...) for (ll i = s, ##args; i < e; i++) #define erg(i, u, args...) for (ll i = vtx[u], ##args; ~i; i = eg[i].nxt) #define fulset(x, v) memset(x, v, sizeof(x))
structitem { int w; int v; };
item a[101]; int dp[101][1010]; int n,cap;
voidbag_01() { fulset(dp, 0); for (int i=1;i<=n;i++) { for (int j=cap;j>=a[i].w;j--) dp[i][j] = max(dp[i-1][j], dp[i-1][j-a[i].w]+a[i].v); for (int j=a[i].w-1;j>=0;j--) dp[i][j] = dp[i-1][j]; } cout << "The max Value is : "; cout << dp[n][cap] << endl; }
voidTrace_back() { bool x[101]; int tmp = cap; fulset(x, false); for (int i=n;i>=2;i--) if (dp[i][tmp]!=dp[i-1][tmp]) { x[i] = true; tmp -= a[i].w; } if (dp[1][tmp]) x[1] = true; cout << "We choose these items:" << endl; for (int i=1;i<=n;i++) if (x[i]) cout << i << " "; cout << endl; }
intmain() { cout << "Please input number of items and the total capacity" << endl; cin >> n >> cap; cout << "Please input their weigts" << endl; for (int i=1;i<=n;i++) cin >> a[i].w; cout << "Please input their values" << endl; for (int i=1;i<=n;i++) cin >> a[i].v; bag_01(); Trace_back(); }