hdu 1423(Greatest Common Increasing Subsequence)最长不降公共子序列

hdu 1423(Greatest Common Increasing Subsequence)最长不降公共子序列
 1 #include <iostream>
 2  using  namespace std;
 3  #define M 505
 4  int num1[M],num2[M];
 5  int dp[M];
 6  int main(){
 7      // freopen("in.txt","r",stdin);
 8       int i,j,k,cas;
 9     scanf("%d",&cas);
10      while (cas--){
11          int m1,m2;
12         scanf("%d",&m1);
13          for(i=1;i<=m1;i++)
14             scanf("%d",&num1[i]);
15         scanf("%d",&m2);
16          for(i=1;i<=m2;i++)
17             scanf("%d",&num2[i]);
18         memset(dp,0, sizeof(dp));
19         dp[0]=-1;
20          for(i=1;i<=m1;i++){ // num1
21              k=0;
22              for(j=1;j<=m2;j++){ // num2
23                   if(num1[i] > num2[j] && dp[j] > dp[k])
24                     k=j;
25                  if(num1[i] == num2[j])
26                     dp[j] = (dp[k] >= 0 ? dp[k] : 0)+1;
27             }
28         }
29         j=m1>m2?m1:m2;
30         k=0;
31          for(i=1;i<=j;i++)
32              if(k<dp[i])
33                 k=dp[i];
34         printf(cas?"%d\n\n":"%d\n",k);
35     }
36      return 0;
37 }

你可能感兴趣的:(hdu 1423(Greatest Common Increasing Subsequence)最长不降公共子序列)