J - Greatest Common Increasing Subsequence

2024-04-11 15:48

本文主要是介绍J - Greatest Common Increasing Subsequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

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

15
1 4 2 5 -12
4
-12 1 2 4

Sample Output

2

题意:

这道题要求两个数列的最长的上升公共子序列;

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;int t,n,m;
int a[1001],b[1001];int dp[1001];int main()
{scanf("%d",&t);while(t--){int i,j;scanf("%d",&n);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);for(i=0;i<n;i++){int max=0;for(j=0;j<m;j++){if(a[i]>b[j]&&max<dp[j])max=dp[j];if(a[i]==b[j])dp[j]=max+1;}}int max=0;for(i=0;i<m;i++){if(dp[i]>max)max=dp[i];}printf("%d\n",max);if(t!=0)printf("\n");}return 0;
}

 

这篇关于J - Greatest Common Increasing Subsequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/894506

相关文章

兔子--Android Studio出现错误:Error:Execution failed for task ':myapp:dexDebug'. com.android.ide.common.pro

重点在:finished with non-zero exit value 2. 这里表明了有重复的内容存在。 由于:Android Studio中引入包的方式有如下2种:    compile 'com.android.support:support-v4:22.0.0'    compile files('libs/support-v

YOLOV5入门教学-common.py文件

在 YOLOv5 框架中,common.py 文件是一个核心组件,负责定义深度学习模型的基础模块和常用操作。无论是卷积层、激活函数、特征融合还是其他复杂的模型结构,common.py 都提供了灵活且高效的实现。在这篇文章中,我们将深入解析 common.py 的设计思想、各个模块的功能以及它在 YOLOv5 中的应用。通过理解该文件的实现细节,不仅可以帮助我们更好地掌握 YOLOv5 的内部结构,

[LeetCode] 300. Longest Increasing Subsequence

题:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目 Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7

[LeetCode] 594. Longest Harmonious Subsequence

题:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目 We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactl

【HDU】4991 Ordered Subsequence 线段树树状数组

传送门:【HDU】4991 Ordered Subsequence 题目分析:本题就是求长度为m的严格上升序列的个数。 就是个DP! 设dp[ i ][ j ]为以位置i的数为结尾的长度为j的严格上升序列的个数。 则dp[ i ][ j ] = sum { dp[ k ][ j - 1 ] | 1 <= k <= i - 1 } 那么如果朴素O(n^2)不超时是不用想了,这样我们该

网络协议栈学习之socket, sock_common, sock, 和 sk_buff

一. 前言   一直很好奇socket是如何实现的,底层的数据结构又是如何,因此在这里对socket的数据结构进行分析。   socket是传输层使用的数据结构,用于声明、定义套接字,网络层会调用sock结构体,其中sock会用到了通用sock_common结构体。而sk_buff则是内核中使用的套接字缓冲区结构体。在我们前文提到的NAT转换中,除了修改内核已有的Netfilter源码外,还有一

Netfilter学习之NAT类型动态配置(八)nf_nat_proto_common.c代码解析

nf_nat_proto_common.c实现了对称型的端口改变,在此我决定对其代码进行分析,以便实现对对称型NAT的随意改动。    具体代码如下: #include <linux/types.h>#include <linux/random.h>#include <linux/netfilter.h>#include <linux/export.h>#include <net/n

CodeForces 490E Restoring Increasing Sequence

题意: 一个严格递增序列  某些数字的某些位被盖住了  求  恢复后的序列 思路: 贪心  让每个数在大于前一个的基础上尽量的小 先讨论数字长度 len[i]<len[i-1] 一定是NO len[i]>len[i-1] 除了第一位如果是?就填1以外  其他?全填0 len[i]==len[i-1] dfs搜索num[i]格式下大于num[i-1]的最小的数 代码: #include

浙大数据结构:01-复杂度2 Maximum Subsequence Sum

数据结构MOOC PTA习题 01-复杂度2 Maximum Subsequence Sum #include <iostream>using namespace std;const int M = 100005;int a[M];int main(){int k;cin >> k;int f = 1;for (int i = 0; i < k; i++){cin >> a[i

Binary Tree - Lowest Common Ancestor 题型总结

Lowest Common Ancestor of a Binary Search Tree 思路:这题跟 Lowest Common Ancestor of Binary Tree 一模一样。思路:就是找p的节点在不在左支,或者右支,找到各自左右节点,然后进行比较,如果两者不一样,说明当前的root就是lowest 父节点,如果左边为空,那就都在右边,返回右边的即可,如果右边为空,那就都在左