HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)
填坑的时候又到啦,校赛因为不会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
#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;
}
HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)的更多相关文章
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU1423 Greatest Common Increasing Subsequence
题意 如标题. \(|s1|,|s2| \leq 500\) 分析 既然是dp问题的组合,那么考虑dp. 定义状态f(i,j)表示对第一个序列s1的前i个和第二个序列s2的前j个元素求最长上升公共子序 ...
- HDU1423 Greatest Common Increasing Subsequence (DP优化)
LIS和LCS的结合. 容易写出方程,复杂度是nm2,但我们可以去掉一层没有必要的枚举,用一个变量val记录前一阶段的最优解,这样优化成nm. 1<=k<j,j增加1,k的上界也增加1,就 ...
- HDU1423:Greatest Common Increasing Subsequence(LICS)
Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- PHPStorm配置支持友好的Laravel代码自动提示
在项目的composer.json "barryvdh/laravel-ide-helper":"dev-master" 项目config/app.php Ba ...
- Ruby on Rail学习笔记
说明:只针对Windows8.1 Windows下,上rubyinstaller上下载最新的railsinstaller包含Ruby2.1的,然后更新gem 用命令: gem update --sys ...
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- [iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端一)
介绍完了服务器,这篇我们就要介绍重点了,写我们自己的IOS客户端程序 先看一下我们完成的效果图 首先下载xmppframework这个框架,下载 点ZIP下载 接下来,用Xcode新建一个工程 将以下 ...
- TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决
TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决 //以下是tcpclient服务器端的监听程序,假设服务器端和客户端在同一台机器上,//为了使客户端可 ...
- c# 各种排序算法+找第二大的数+句子单词反转
冒泡排序 // 冒泡排序 bubble sort public static int[] BubbleSort(int []array) { bool isContinue = true; ; i & ...
- 5方与5W
方案:方位(角色).方向.方针.方式.方法 解答5W2H 轻重.缓急.深浅
- mobiscroll 控件的使用(手机端日历控件)
先上一张图吧: 控件的下载地址:http://www.kuitao8.com/20140604/2615.shtml 文档API地址:http://docs.mobiscroll.com/2-13-2 ...
- eclipse for jee版配置tomcat
在网上搜到的大多都是插件配置,其实默认的就可以配置tomcat的. 第一步:New -> Other -> Server ,然后选择Apache下的tomcat的版本. 注意:如果Next ...
- 学习Linux第六天
1.Shell编程 bash变量: 都是以字符串格式存储 x=5 等号左右不能有空格,会当作命令处理 如何调用: echo $x 此法无法进行数值运算,不存在的变量输出空 set -u 设置变量报错 ...