HDU1423-Greatest Common Increasing Subsequence
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
题意:
求出给定的两个数组的最长公共上升子序列,输出最长长度即可。
dp记录的是最长长度,记得清空
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[520],b[520],dp[520]; 10 11 int main() 12 { 13 int tt,p,q; 14 scanf("%d",&tt); 15 while(tt--) 16 { 17 memset(dp,0,sizeof(dp)); 18 scanf("%d",&p); 19 for(int i=0;i<p;i++) 20 scanf("%d",&a[i]); 21 scanf("%d",&q); 22 for(int i=0;i<q;i++) 23 scanf("%d",&b[i]); 24 int maxx; 25 for(int i=0;i<p;i++) 26 { 27 maxx=0;//注意一下,这里不能设置为负无穷,因为dp记录最长长度,最小是为0 28 for(int j=0;j<q;j++) 29 { 30 if(a[i]>b[j]) 31 maxx=max(dp[j],maxx); 32 else if(a[i]==b[j]) 33 dp[j]=maxx+1; 34 } 35 } 36 int ans=0; 37 for(int i=0;i<q;i++) 38 ans=max(ans,dp[i]); 39 if(tt) 40 printf("%d\n\n",ans); 41 else 42 printf("%d\n",ans); 43 } 44 return 0; 45 }
分类:
数论
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」