zoukankan      html  css  js  c++  java
  • ZOJ 2432 Greatest Common Increasing Subsequence

    LCIS+记录路径

    LCIS:
    dp[i][j] 表示数列a前i项,和数列b前j项且以b[ j ]结尾的LCIS。
    当a[ i ]!= b[ j ]时,dp[ i ][ j ]==dp[i-1][ j ]
    当a[ i ]==b[ j ]时,dp[ i ][ j ]==dp[i-1][1~j之间最大的] +1

    最后 统计 dp[len(a)][...]里最大的就是LICS

    Greatest Common Increasing Subsequence

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) :    Accepted Submission(s) : 
    Special Judge
    Problem Description
    You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal 
    possible length. Sequence S1, S2, ..., SN of length N is called an increasing subsequence of a sequence A1, A2, ..., AM of length M if there exist 1 <= i1 < i2 < ...< iN <= M such that Sj = Aij for all 1 <= j <= N, and Sj < Sj+1 for all 1 <= j < N. 


    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

    On the first line of the output print L - the length of the greatest common increasing subsequence of both sequences. On the second line print the subsequence itself. If there are several possible answers, output any of them.


    This problem contains multiple test cases!

    The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

    The output format consists of N output blocks. There is a blank line between output blocks.


    Sample Input

    1

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


    Sample Output

    2
    1 4

     
     1 #include <iostream>
     2 #include <cstring>
     3 
     4 using namespace std;
     5 
     6 const int N=555;
     7 
     8 struct p
     9 {
    10     int x,y;
    11 }path[N][N];
    12 
    13 int a[N],b[N],dp[N][N];
    14 
    15 int main()
    16 {
    17     int t;
    18     cin>>t;
    19 while(t--)
    20 {
    21     int n,m;
    22     cin>>n;
    23     for(int i=1;i<=n;i++)
    24         cin>>a[i];
    25     cin>>m;
    26     for(int j=1;j<=m;j++)
    27         cin>>b[j];
    28 
    29     int maxn=0;
    30     memset(dp,0,sizeof(dp));
    31     for(int i=1;i<=n;i++)
    32     {
    33         maxn=0;
    34         int tx=0,ty=0;
    35         for(int j=1;j<=m;j++)
    36         {
    37             dp[i][j]=dp[i-1][j];
    38             path[i][j].x=i-1;
    39             path[i][j].y=j;
    40             if(a[i]>b[j]&&maxn<dp[i-1][j])
    41             {
    42                 maxn=dp[i-1][j];
    43                 tx=i-1;ty=j;
    44             }
    45             if(a[i]==b[j])
    46             {
    47                 dp[i][j]=maxn+1;
    48                 path[i][j].x=tx;
    49                 path[i][j].y=ty;
    50             }
    51         }
    52     }
    53 
    54     maxn=-1;
    55     int id;
    56     for(int i=1;i<=m;i++)
    57     {
    58         if(maxn<dp[n][i])
    59         {
    60             maxn=dp[n][i];
    61             id=i;
    62         }
    63     }
    64 
    65     int save[N];
    66     int cnt=0;
    67     int tx=n,ty=id;
    68     while(dp[tx][ty]!=0)
    69     {
    70         int tmpx=path[tx][ty].x;
    71         int tmpy=path[tx][ty].y;
    72 
    73         if(dp[tmpx][tmpy]!=dp[tx][ty])
    74         {
    75             save[cnt++]=b[ty];
    76         }
    77 
    78         tx=tmpx;  ty=tmpy;
    79     }
    80 
    81     cout<<maxn<<endl;
    82     for(int i=cnt-1;i>=0;i--)
    83     {
    84         cout<<save[i]<<" ";
    85     }
    86     cout<<endl;
    87 }
    88 
    89     return 0;
    90 }
  • 相关阅读:
    引擎设计跟踪(九.14.2h) 开发计划
    经典游戏重温
    引擎设计跟踪(九.14.2g) 将GNUMake集成到Visual Studio
    [工作积累] Android system dialog with native callback
    [工作积累] Software keyboard not shown on Activity.onCreate
    [工作积累] Android: Hide Navigation bar 隐藏导航条
    [工作积累] GCC 4.6 new[] operator内存对齐的BUG
    引擎设计跟踪(九.14.2f) 最近更新: OpenGL ES & tools
    [工作积累] Google Play Game SDK details
    [工作积累] Android dynamic library & JNI_OnLoad
  • 原文地址:https://www.cnblogs.com/CKboss/p/3092255.html
Copyright © 2011-2022 走看看