zoukankan      html  css  js  c++  java
  • zoj 2432 模板LCIS

    输出一条 LCIS

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 int T;
     4 int len1, len2;
     5 int s1[510], s2[510];
     6 int dp[510][510];
     7 int choice[510][510];
     8 
     9 
    10 int main()
    11 {
    12     scanf("%d", &T);
    13     while(T--) {
    14         memset(dp, 0, sizeof(dp));
    15         memset(choice, 0, sizeof(choice));
    16         scanf("%d", &len1);
    17         int i, j;
    18         for(i = 1; i <= len1; ++i) {
    19             scanf("%d", &s1[i]);
    20         }
    21         scanf("%d", &len2);
    22         for(j = 1; j <= len2; ++j) {
    23             scanf("%d", &s2[j]);
    24         }
    25 
    26         for(i = 1; i <= len1; ++i) {
    27             int k = 0;
    28             for(j = 1; j <= len2; ++j) {
    29                 if(s1[i] == s2[j]) {
    30                     dp[i][j] = dp[i - 1][k] + 1;
    31                     choice[i][j] = 1000 * (i - 1) + k;
    32                 }
    33                 else {
    34                     dp[i][j] = dp[i - 1][j];
    35                     choice[i][j] = choice[i - 1][j];
    36                     if(s1[i] > s2[j] && dp[i - 1][k] < dp[i - 1][j]) {
    37                         k = j;
    38                     }
    39                 }
    40 //                printf("dp[%d][%d] == %d
    ", i ,j, dp[i][j]);
    41             }
    42         }
    43 
    44         int end_pos;
    45         int res = 0;
    46         for(j = 1; j <= len2; ++j) {
    47             if(res < dp[len1][j]) {
    48                 res = dp[len1][j];
    49                 end_pos = j;
    50             }
    51         }
    52         printf("%d
    ", res);
    53         if(res == 0) {
    54             continue;
    55         }
    56         int state_1, state_2, tmp;
    57         state_1 = len1;
    58         state_2 = end_pos;
    59         int tot_ans = 0;
    60         while(state_2) {
    61             ++tot_ans;
    62             s1[tot_ans] = s2[state_2];
    63             tmp = choice[state_1][state_2];
    64             state_1 = tmp / 1000;
    65             state_2 = tmp % 1000;
    66         }
    67         while(tot_ans) {
    68             printf("%d ", s1[tot_ans]);
    69             --tot_ans;
    70         }
    71         printf("
    ");
    72     }
    73 }
    74 //2
    75 //5
    76 //1 4 2 5 -12
    77 //4
    78 //-12 1 2 4
    79 
    80 
    81 //2
    82 //5
    83 //1 2 3 4 5
    84 //1
    85 //9
  • 相关阅读:
    php mysql 查询
    正则表达式 常用匹配模式
    正则 去除html标签
    PHP 操作MySQL
    MySQL 的中文乱码问题终结
    [转]BP人工神经网络的介绍与实现
    [转]BP人工神经网络的C++实现
    [转]高效BP(反向传播算法)
    [转]反向传播算法BP (弄懂为什么是反向而不是正向)
    [转]BP神经网络梯度下降算法
  • 原文地址:https://www.cnblogs.com/AC-Phoenix/p/4655615.html
Copyright © 2011-2022 走看看