HDU-1423-Greatest Common Increasing Subsequence-最长公共上升子序列【模版】

This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.

InputEach sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.Outputoutput 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

注意一下控制格式
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<iomanip>
 6 #include<string.h>
 7 using namespace std;
 8
 9 int a[550];
10 int b[550];
11 int dp[550];
12
13 int main()
14 {
15     std::ios::sync_with_stdio(false);
16     cin.tie(0);
17     cout.tie(0);
18     int tt;
19     cin>>tt;
20     while(tt--)
21     {
22         memset(a,0,sizeof(a));
23         memset(b,0,sizeof(b));
24         memset(dp,0,sizeof(dp));
25         int p,q;
26         cin>>p;
27         for(int i=0;i<p;i++)
28             cin>>a[i];
29         cin>>q;
30         for(int i=0;i<q;i++)
31             cin>>b[i];
32         int maxx;
33         for(int i=0;i<p;i++)
34         {
35             maxx=0;//每次都要恢复
36             for(int j=0;j<q;j++)
37             {
38                 if(a[i]>b[j])
39                     maxx=max(dp[j],maxx);
40                 else if(a[i]==b[j])
41                     dp[j]=maxx+1;
42             }
43         }
44         int ans=0;
45         for(int i=0;i<q;i++)
46             ans=max(ans,dp[i]);
47         if(tt)
48             cout<<ans<<endl<<endl;
49         else
50             cout<<ans<<endl;
51     }
52     return 0;
53 }


原文地址:https://www.cnblogs.com/OFSHK/p/11235714.html

时间: 2024-08-01 16:45:28

HDU-1423-Greatest Common Increasing Subsequence-最长公共上升子序列【模版】的相关文章

HDU 1403 Longest Common Substring(最长公共前缀)

http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意:给出两个字符串,求最长公共子串的长度. 思路: 刚开始学后缀数组,确实感觉很难,但是这东西很强大,所以必须要学会它,推荐罗穗骞大牛的论文. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #i

dp(最长公共上升子序列)

This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence. InputEach sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.Outpu

HDU1423 最长公共上升子序列LCIS

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

POJ 1458 Common Subsequence(最长公共子序列LCS)

POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列长度. 分析: 本题不用输出子序列,非常easy,直接处理就可以. 首先令dp[i][j]==x表示A串的前i个字符和B串的前j个字符的最长公共子序列长度为x. 初始化: dp全为0. 状态转移: IfA[i]==B[j] then dp[i][j]= dp[i-1][j-1]+1 else dp[

hdu 1160 FatMouse&#39;s Speed(最长不下降子序列+输出路径)

题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the s

【LeetCode-面试算法经典-Java实现】【014-Longest Common Prefix(最长公共前缀)】

[014-Longest Common Prefix(最长公共前缀)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Write a function to find the longest common prefix string amongst an array of strings. 题目大意 写一个函数找出一个字串所数组中的最长的公共前缀. 解题思路 第一步先找出长度最小的字符串,然后将这个字符串与其它的字符串相比找出最短的最公共前缀. 代码实现 publi

HDU 4512 最长公共上升子序列

各种序列复习: (1)最长上升子序列. 1.这个问题用动态规划就很好解决了,设dp[i]是以第i个数字结尾的上升子序列的最长长度.那么方程可以是dp[i]=max(dp[j]+1).(j<i).复杂度为O(n^2); 2.另外有一个该经典问题的O(nlogn)算法. 首先知道,当求dp[i]时,如果出现a[k]<a[j],而dp[k]=dp[j]时,应当优先选k吧.那么,既然每次选的都是较小,就可以把字符串按照dp[t]=k这个子序列长度分类.当同样dp[t]=k时,记录下该长度的最小的a[p

HDU 4512 吉哥系列故事——完美队形I(LCIS最长公共上升子序列)

http://acm.hdu.edu.cn/showproblem.php?pid=4512 题意: 吉哥这几天对队形比较感兴趣. 有一天,有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求,则称之为完美队形: 1.挑出的人保持他们在原队形的相对顺序不变: 2.左右对称,假设有m个人形成新的队形,则第1个人和第m个人身高相同,第2个人和第m-1个人身高相同,依此类推,当然,如果m是奇数,

HDU ACM 4512 吉哥系列故事——完美队形I -&gt;LCIS最长公共递增子序列

分析:最长公共递增子序列,把数据反向存储一遍,求正反两组数据的LCIS.另外注意边界的条件判断.还有如果取出的新队列有奇数个人或偶数个人要单独判断. #include<iostream> using namespace std; #define max(a,b) ((a)>(b)?(a):(b)) int dp[202]; int a[202]; int b[202]; int LCIS(int n) { int i,j,maxlen,ans; memset(dp,0,sizeof(dp