zoukankan      html  css  js  c++  java
  • POJ 2127 Greatest Common Increasing Subsequence

    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 (-231 <= Ai < 231 ) --- the sequence itself.

    Output

    On the first line of the output file 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.

    Sample Input

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

    Sample Output

    2
    1 4

    题意:求最长上升公共子序列(LCIS),并记录路径.
    状态:dp[i][j]表示以s1串的前i个字符s2串的前j个字符且以s2[j]为结尾构成的LCIS的长度。

    状态转移:当 s1[i-1]!=s2[j-1]时,按照lcs可知由2个状态转移过来,dp[i-1][j],dp[i][j-1],因为dp[i][j]是以s2[j]为结尾构成的LCIS的长度。所以s2[j-1]一定会包含在里面,所以舍去dp[i][j-1],只由dp[i-1][j] 转移过来。当s1[i-1]==s2[j-1],这时肯定要找前面s1[ii-1]==s2[jj-1]的最长且比s2[j-1]小的状态转移过来.

    若s1[i-1]!=s2[j-1] 那么dp[i][j]=dp[i][j-1]

    若s1[i-1]==s2[j-1] 那么dp[i][j]=MAX{dp[i-1][k]+1(0<k<j),s2[k-1]<s2[j-1]};

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define N 505
     4 
     5 int i, j, n, m, len;
     6 int s1[N], s2[N], rd[N][N], dp[N][N];
     7 
     8 void DP()
     9 {
    10     int mac = 0, ans = 0, I, J, lu[N], mx;
    11     for(i = 1; i <= n; i++){
    12         mx = 0;
    13         for(j = 1; j <= m; j++){
    14             int tem = dp[i][j] = dp[i-1][j];
    15             if(mx < tem && s1[i-1] > s2[j-1]){
    16                 mx = tem;
    17                 mac = j;
    18             }
    19             if(s1[i-1] == s2[j-1]){
    20                 dp[i][j] = mx + 1;
    21                 rd[i][j] = mac;
    22             }
    23             if(ans < dp[i][j]){
    24                 ans = dp[i][j];
    25                 I = i, J = j;
    26             }
    27         }
    28     }
    29     printf("%d
    ", ans);
    30     len = ans;
    31     if(ans > 0)
    32         lu[ans--] = J-1;
    33     while(ans && I && J){
    34         if(rd[I][J] > 0){
    35             lu[ans--] = rd[I][J] - 1;
    36             J = rd[I][J];
    37         }
    38         I--;
    39     }
    40     for(i = 1; i <= len; i++)
    41         printf("%d ",s2[lu[i]]);
    42     printf("
    ");
    43 }
    44 
    45 int main()
    46 {
    47     scanf("%d", &n);
    48     for(i = 0; i < n; i++)
    49         scanf("%d", &s1[i]);
    50     scanf("%d", &m);
    51     for(i = 0; i < m; i++)
    52         scanf("%d", &s2[i]);
    53     DP();
    54     return 0;
    55 }
  • 相关阅读:
    成都Uber优步司机奖励政策(2月18日)
    北京Uber优步司机奖励政策(2月18日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(2月18日)
    优步UBER司机全国各地最新奖励政策汇总(持续更新...)
    新年Uber司机端换新装啦!
    百度参投 Uber中国12亿美元融资已到账
    人民优步+(拼车)自动接单功能上线
    vi 调到第一行,或最后一行
    linux文本操作界面 vi面板如何复制一行
    Apache支持多端口配置处理
  • 原文地址:https://www.cnblogs.com/zzy9669/p/3867516.html
Copyright © 2011-2022 走看看