HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)
2014-05-09 20:48
447 查看
填坑的时候又到啦,校赛因为不会LCIS所以吃了大亏,这里要补起来。LCIS就是在两个串里找最长上升子序列,相关的博客有很多,这里自己就不写那么多了。
http://www.cnblogs.com/jackge/archive/2013/05/16/3081793.html http://www.cnblogs.com/gj-Acit/p/3236384.html
上面两个博客对于O(n^2)的做法讲解的比较详细,大家可以参考一下。
贴两记代码
HDU1423
HDU4512
http://www.cnblogs.com/jackge/archive/2013/05/16/3081793.html http://www.cnblogs.com/gj-Acit/p/3236384.html
上面两个博客对于O(n^2)的做法讲解的比较详细,大家可以参考一下。
贴两记代码
HDU1423
#pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string> #include <vector> #include <algorithm> #include <cstdio> using namespace std; #define maxn 550 int a[maxn]; int b[maxn]; int n1, n2;; int dp[maxn][maxn]; int main() { int T; cin >> T; while (T--){ cin >> n1; for (int i = 1; i <= n1; i++) scanf("%d", a + i); cin >> n2; for (int i = 1; i <= n2; i++) scanf("%d", b + i); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n1; i++){ int tmp = 0; for (int j = 1; j <= n2; j++){ dp[i][j] = dp[i - 1][j]; if (a[i] > b[j] && dp[i - 1][j] > tmp) tmp = dp[i - 1][j]; if (a[i] == b[j]) dp[i][j] = tmp + 1; } } int ans = 0; for (int i = 1; i <= n1; i++){ ans = max(ans, dp[n1][i]); } printf("%d\n", ans); if (T) puts(""); } return 0; }
HDU4512
#pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string> #include <vector> #include <algorithm> #include <cstdio> using namespace std; #define maxn 550 int a[maxn]; int b[maxn]; int n; int dp[maxn][maxn]; int main() { int T; cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++){ scanf("%d", a + i); b[n + 1 - i] = a[i]; } int ans = 0; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++){ int tmp = 0; for (int j = 1; j <= n+1-i; j++){ dp[i][j] = dp[i - 1][j]; if (a[i] > b[j] && dp[i - 1][j] > tmp) tmp = dp[i - 1][j]; if (a[i] == b[j]){ dp[i][j] = max(dp[i][j],tmp + 1); } if (i < n + 1 - j) ans = max(ans, dp[i][j] * 2); else ans = max(ans, dp[i][j] * 2 - 1); } } printf("%d\n", ans); } return 0; }
相关文章推荐
- Greatest Common Increasing Subsequence(hdu1423(LCIS))
- HDU1423--Greatest Common Increasing Subsequence(LCIS)
- zoj 2432 && hdoj 1423 Greatest Common Increasing Subsequence 【打印LCIS】
- hdu1423 Greatest Common Increasing Subsequence(最长递增公共子序列)
- poj 2127 Greatest Common Increasing Subsequence (LCIS)
- hdu1423 Greatest Common Increasing Subsequence
- HDU1423----Greatest Common Increasing Subsequence
- HDU1423:Greatest Common Increasing Subsequence
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
- 1423 Greatest Common Increasing Subsequence (LCIS)
- 【HDU 1423】Greatest Common Increasing Subsequence【LCIS 裸题】
- POJ 2127 Greatest Common Increasing Subsequence(DP,LCIS)
- hdu 1423 Greatest Common Increasing Subsequence(LCIS)
- HDU 1423 Greatest Common Increasing Subsequence LCIS
- hdu 1423 Greatest Common Increasing Subsequence_LCIS
- HDU 1423--Greatest Common Increasing Subsequence【LCIS】
- 最长公共上升子序列——hdu1423 Greatest Common Increasing Subsequence
- HDU1423 Greatest Common Increasing Subsequence(最长公共递增子序列)
- LCIS POJ 2172 Greatest Common Increasing Subsequence