HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8768 Accepted Submission(s): 2831
Problem Description
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.
Input
Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.
Output
output print L - the length of the greatest common increasing subsequence of both sequences.
Sample Input
1
5
1 4 2 5 -12
4
-12 1 2 4
Sample Output
2
题意
找出两个序列中的最长公共上升子序列
思路
动态规划
假如 a[i] != b[j]
那么毫无疑问 a[i] 对这个LCIS是毫无贡献的 所以 dp[i][j] = dp[i - 1][j];
如果a[i] == b[j]
那么 这个最长公共上升子序列的长度至少为1 并且 找出前面可以接的最长的LCIS的长度 + 1 就可以了
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e2 * 5 + 5;
const int MOD = 1e9 + 7;
int a[maxn], b[maxn], dp[maxn][maxn];
int main()
{
int t;
cin >> t;
while (t--)
{
int n, m;
scanf(" %d", &n);
int i, j, k;
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
for (i = 0; i < m; i++)
scanf("%d", &b[i]);
memset(dp, 0, sizeof(dp));
int ans = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (a[i] == b[j])
{
int max_dp = 0;
for (k = 0; k < j; k++)
{
if (dp[i][k] > max_dp && b[j] > b[k])
max_dp = dp[i][k];
}
dp[i][j] += max_dp + 1;
}
else if (i)
dp[i][j] = dp[i - 1][j];
if (dp[i][j] > ans)
ans = dp[i][j];
}
}
cout << ans << endl;
if (t)
cout << endl;
}
}
优化代码
#include <iostream> //时间优化
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e2 * 5 + 5;
const int MOD = 1e9 + 7;
int a[maxn], b[maxn], dp[maxn][maxn];
int main()
{
int t;
cin >> t;
while (t--)
{
int n, m;
scanf(" %d", &n);
int i, j, k;
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
for (i = 0; i < m; i++)
scanf("%d", &b[i]);
memset(dp, 0, sizeof(dp));
int ans = 0;
int max_dp;
for (i = 0; i < n; i++)
{
max_dp = 0;
for (j = 0; j < m; j++)
{
if (i)
dp[i][j] = dp[i - 1][j];
if (a[i] > b[j] && dp[i][j] > max_dp)
max_dp = dp[i][j];
if (a[i] == b[j])
dp[i][j] = max_dp + 1;
if (dp[i][j] > ans)
ans = dp[i][j];
}
}
cout << ans << endl;
if (t)
cout << endl;
}
}
HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】的更多相关文章
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDOJ 1423 Greatest Common Increasing Subsequence(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 思路分析:[问题定义]给定两个序列A[0, 1,..., m]和B[0, 1, ..., n], ...
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- [Tips]Javascrip计算文件行数
function calcLineCount(filename, callback) { var fs = require('fs'); var fileStream = fs.createReadS ...
- 模式识别之贝叶斯---朴素贝叶斯(naive bayes)算法及实现
处女文献给我最喜欢的算法了 ⊙▽⊙ ---------------------------------------------------我是机智的分割线----------------------- ...
- 多线程之使用读写锁ReentrantReadWriteLock实现缓存系统
简单地缓存系统:当有线程来取数据时.假设该数据存在我的内存中.我就返回数据.假设不存在我的缓存系统中,那么就去查数据库.返回数据的同一时候保存在我的缓存中. 当中涉及到读写问题:当多个线程运行读操作时 ...
- (转)Java锁、自旋锁、CAS机制
转自:http://www.jb51.net/article/55381.htm 转自:http://blog.csdn.net/aesop_wubo/article/details/7537278 ...
- Chrome 新建自定义标签
刚开始用Chrome让我最不爽的地方有2个: 1.不能双击关闭标签 2.新建的标签不能是自定义网页 第一个问题通过插件解决了,第二个问题今天也总算找到解决的方法了. 这个方法是自定义插件,需要2个文件 ...
- 【转】CStdioFile UNICODE编译 英文系统下读取中文汉字乱码解决
转载出处:http://www.cnblogs.com/ct0421/p/3242418.html 函数原形为:char *setlocale( int category, const char *l ...
- Jmeter常用函数
一._csvRead 函数 _cvsRead函数是从外部读取参数,csvRead函数可以从一个文件中读取多个参数. 步骤: 1.先新建一个文件,例如c.txt,里面的数据存放为 web@qq.com, ...
- mysql 中调用存储过程之后,连接断开不可用
解决方法: 由 mysql_real_connect(&m_mysql,host,user,passwd,Db,0,NULL,0) == NULL 改为 mysql_real_connect( ...
- info 手册
info flex 可以查看flex帮助. h就可以看到相关命令,常用命令已经加粗: x 关闭此帮助窗口. q 一并退出 Info. RET ...
- Python菜鸟之路:Django 缓存
Django中提供了6种缓存方式: 开发调试 内存 文件 数据库 Memcache缓存(python-memcached模块) Memcache缓存(pylibmc模块) 1. 开发调试 # 此为开始 ...