HDU 1423 Greatest Common Increasing Subsequence

题目链接

题目直接说明题意了,求最长上升的公共子序。开一个标记数组,标记上一个匹配的数字就行。

这个题坑爹的是输出格式,题目中根本没有说明,不过,猜猜就知道是每个样例之间有空行,还果真是。。。

PS:这样是瞎搞过的。。。。杭电数据水了。。。2012.8.13

PS:这份代码是正解,这个坑过了好久了,才来添。。。其实就是把dp[i][j][k],然后多一维记录上升长度为k,最小的数是多少。。。然后+各种优化,时间复杂度降低到O(n^2),然后空间复杂度也降了很多。。。2012.11.6

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 int dp[501];
 5 int main()
 6 {
 7     int i,j,t,len1,len2,temp,ans;
 8     int str1[501],str2[501];
 9     scanf("%d",&t);
10     while(t--)
11     {
12         memset(dp,0,sizeof(dp));
13         scanf("%d",&len1);
14         for(i = 1; i <= len1; i ++)
15             scanf("%d",&str1[i]);
16         scanf("%d",&len2);
17         for(i = 1; i <= len2; i ++)
18             scanf("%d",&str2[i]);
19         ans = 0;
20         for(i = 1;i <= len1;i ++)
21         {
22             temp = 0;
23             for(j = 1;j <= len2;j ++)
24             {
25                 if(str2[j] < str1[i])
26                 {
27                     if(dp[j] > temp)
28                     temp = dp[j];
29                 }
30                 else if(str2[j] == str1[i])
31                 {
32                     dp[j] = temp+1;
33                     if(ans < dp[j])
34                     ans = dp[j];
35                 }
36             }
37             for(j = 1;j <= len2;j ++)
38             printf("%d ",dp[j]);
39             printf("\n");
40         }
41         printf("%d\n",ans);
42         if(t != 0)
43         printf("\n");
44     }
45     return 0;
46 }

以下代码虽然能AC,但是是错的。。。乱搞过的。

 1 #include <stdio.h>
 2 #include <string.h>
 3 int p[501][501],o[501][501];
 4 int main()
 5 {
 6     int i,j,t,n,len1,len2,z;
 7     int str1[501],str2[501];
 8     scanf("%d",&t);
 9     while(t--)
10     {
11         memset(p,0,sizeof(p));
12         memset(o,0,sizeof(o));
13         scanf("%d",&len1);
14         for(i = 1; i <= len1; i ++)
15             scanf("%d",&str1[i]);
16         scanf("%d",&len2);
17         for(i = 1; i <= len2; i ++)
18             scanf("%d",&str2[i]);
19         for(i = 1; i <= len1; i ++)
20             for(j = 1; j <= len2; j ++)
21             {
22                 z = 1;
23                 if(str1[i] == str2[j])
24                 {
25                     if(p[i-1][j-1] == 0)
26                     {
27                         p[i][j] = p[i-1][j-1]+1;
28                         o[i][j] = str1[i];
29                         z = 0;
30                     }
31                     else
32                     {
33                         if(str1[i] > o[i-1][j-1])
34                         {
35                             p[i][j] = p[i-1][j-1]+1;
36                             o[i][j] = str1[i];
37                             z = 0;
38                         }
39                     }
40                 }
41                 if(z)
42                 {
43                     if(p[i][j-1] > p[i-1][j])
44                     {
45                         p[i][j] = p[i][j-1];
46                         o[i][j] = o[i][j-1];
47                     }
48                     else
49                     {
50                         p[i][j] = p[i-1][j];
51                         o[i][j] = o[i-1][j];
52                     }
53                 }
54             }
55         printf("%d\n",p[len1][len2]);
56         if(t != 0)
57         printf("\n");
58     }
59     return 0;
60 }
posted @ 2012-07-25 16:27  Naix_x  阅读(162)  评论(5编辑  收藏  举报