hdu 1423 Greatest Common Increasing Subsequence
Greatest Common Increasing Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3079 Accepted Submission(s): 969
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
Source
ACM暑期集训队练习赛(二)
Recommend
lcy
//0MS 1248K 1342 B C++ //最长公共递增子序列 //先求出公共子序列再从公共子序列中求递增子序列 #include<stdio.h> #include<string.h> int a[505],b[505]; int dp[505][505]; int Max(int a,int b) { return a>b?a:b; } int main(void) { int t,n,m; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(int i=0;i<m;i++) scanf("%d",&b[i]); int ans[505];//存放公共子序列 memset(ans,-1,sizeof(ans)); for(int i=1;i<=n;i++){ int temp=0; for(int j=1;j<=m;j++){ if(a[i-1]==b[j-1]){ dp[i][j]=Max(dp[i][j],dp[i-1][j-1]+1); if(ans[dp[i][j]]==-1) ans[dp[i][j]]=a[i-1]; } else { dp[i][j]=Max(dp[i-1][j],dp[i][j-1]); } } } int maxn=0; int dp0[505]={0}; for(int i=1;i<=dp[n][m];i++){ //printf("%d\n",ans[i]); int temp=0; for(int j=1;j<i;j++) if(ans[i]>ans[j] && temp<dp0[j]) temp=dp0[j]; dp0[i]=temp+1; if(dp0[i]>maxn) maxn=dp0[i];; } printf("%d\n",maxn); if(t) printf("\n"); //题目没说明错了两次 } return 0; }
1 //0MS 256K 926 B C++ 2 //借鉴别人的直接解法(模板) 3 //直接求最长公共递增子序列数 4 #include<stdio.h> 5 #include<string.h> 6 int a[505],b[505]; 7 int dp[505]; //记录最长公共递增子序列数 8 int main(void) 9 { 10 int t,n,m; 11 scanf("%d",&t); 12 while(t--) 13 { 14 memset(dp,0,sizeof(dp)); 15 scanf("%d",&n); 16 for(int i=0;i<n;i++) 17 scanf("%d",&a[i]); 18 scanf("%d",&m); 19 for(int i=0;i<m;i++) 20 scanf("%d",&b[i]); 21 int maxn=0; 22 for(int i=0;i<n;i++){ 23 int temp=0; 24 for(int j=0;j<m;j++){ 25 if(b[j]<a[i] && dp[j]>dp[temp]) temp=j; 26 if(a[i]==b[j]) dp[j]=dp[temp]+1; 27 //printf("%d %d %d\n",i,j,dp[j]); 28 } 29 } 30 for(int i=0;i<m;i++) 31 if(dp[i]>maxn) maxn=dp[i]; 32 printf("%d\n",maxn); 33 if(t) printf("\n"); 34 } 35 return 0; 36 }