poj2250 最长上升子序列 + 输出

 1 //Accepted    208 KB    0 ms
 2 //最长公共上升子序列+输出
 3 //dp
 4 //输出时用的递归输出,注意条件判断
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <iostream>
 8 using namespace std;
 9 const int imax_n = 105;
10 const int imax_stringlen = 32;
11 char s1[imax_n][imax_stringlen];
12 char s2[imax_n][imax_stringlen];
13 int dp[imax_n][imax_n];
14 int n1,n2;
15 int max(int a,int b)
16 {
17     return a>b?a:b;
18 }
19 void Dp()
20 {
21     memset(dp,0,sizeof(dp));
22     for (int i=1;i<=n1;i++)
23     {
24         for (int j=1;j<=n2;j++)
25         {
26             dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
27             if (strcmp(s1[i],s2[j])==0)
28             dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
29         }
30     }
31 }
32 void output(int i,int j)
33 {
34     if (dp[i][j]==0) return ;
35     if (dp[i-1][j]==dp[i][j])
36     {
37         output(i-1,j);
38         return ;
39     }
40     if (dp[i][j-1]==dp[i][j])
41     {
42         output(i,j-1);
43         return ;
44     }
45     if (dp[i-1][j-1]+1==dp[i][j])
46     {
47         output(i-1,j-1);
48         if (dp[i][j]==1)
49         {
50             printf("%s",s1[i]);
51             return ;
52         }
53         else
54         {
55             printf(" %s",s1[i]);
56             return ;
57         }
58     }
59     return ;
60 }
61 int main()
62 {
63     while (scanf("%s",s1[1])!=EOF)
64     {
65         int k=1;
66         k++;
67         while (scanf("%s",s1[k]) && s1[k][0]!=‘#‘)
68         k++;
69         n1=k-1;
70         k=1;
71         while (scanf("%s",s2[k]) && s2[k][0]!=‘#‘)
72         k++;
73         n2=k-1;
74         Dp();
75         output(n1,n2);
76         printf("\n");
77     }
78     return 0;
79 }

poj2250 最长上升子序列 + 输出

时间: 2024-08-30 08:23:49

poj2250 最长上升子序列 + 输出的相关文章

最长上升子序列输出序列贪心做法

之前做过的最长上升子序列的题只是不需要输出这个序列http://www.cnblogs.com/Scale-the-heights/p/4333346.html 做法就是从左到右扫一遍,可以参见http://blog.csdn.net/shuangde800/article/details/7474903 要输出路径其实也很简单,就开个数组f[]把所有数字在最长上升子序列中第一次出现的位置记录下来,然后逆序遍历,比如我们找到最长上升子序列的长度为n,则我们从后往前找到曾经在最长上升子序列中位置为

最长公共子序列——输出

首先我不得不说这道题很傻逼....你要先求公共子序列的长度......然后去DFS一遍注意要倒着搜......公共子序列也要倒着找..........我做了好久,代码: #include<bits/stdc++.h> using namespace std; char a[1001],b[1001]; int dp[1001][1001]={0}; int n1,n2,sum=0; int ans[1001]={0}; int a1[1001][101],b1[1001][101]; int

hdu 1160 FatMouse&#39;s Speed(最长不下降子序列+输出路径)

题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the s

编程之美-数组中最长递增子序列(包括输出)

#include <iostream> #define N 8 using namespace std; int main(){ int a[N]={1,-1,2,-3,4,-5,6,-7}; int lis[N]; int result[N];//结果 for(int i=0;i<N;i++) result[i]=0; for(int i=0;i<N;i++) { lis[i]=1; for (int j=0;j<i; j++) { if( a[i]> a[j] &a

51nod_1006 最长公共子序列,输出路径【DP】

题意: 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这两个字符串最长的子序列. 输出最长的子序列,如果有多个,随意输出1个. 思路: DP,同时DP记录路径. 代码: string a,b; int f[1005][1005]; int path[1005][1005]; void _print(int x,int y){ if(!x||!y) ret;

最长递增子序列(输出最长递增序列 及其长度)

最长递增子序列的解法有很多种,常用的有最长公共子序列法.动态规划.记录所有递增序列长度最大值的方法. 最长公共子序列法:如例子中的数组A{5,6, 7, 1, 2, 8},则我们排序该数组得到数组A‘{1, 2, 5, 6, 7, 8},然后找出数组A和A’的最长公共子序列即可.显然这里最长公共子序列为{5, 6, 7, 8},也就是原数组A最长递增子序列. 在http://blog.csdn.net/yysdsyl/article/details/4226630中有详细解释. 动态规划:参见h

14-高效求最长公共子序列(二维数组存不下)

/*                                   See LCS again时间限制:1000 ms  |  内存限制:65535 KB难度:3 描述 There are A, B two sequences, the number of elements in the sequence is n.m; Each element in the sequence are different and less than 100000. Calculate the length

NYOJ 36 &amp;&amp;HDU 1159 最长公共子序列(经典)

链接:click here 题意:tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最长公共子序列. 输入 第一行给出一个整数N(0<N<100)表示待测数据组数 接下来每组数据两行,分别为待测的两组字符串.每个字符串长度不大于1000. 输出 每组测试数据输出一个整数,表示最长公共子序列长度.每组

动态规划(4)——最长上升子序列(作业题NYOJ201)

作业题 描述 小白同学这学期有一门课程叫做<数值计算方法>,这是一门有效使用数字计算机求数学问题近似解的方法与过程,以及由相关理论构成的学科-- 今天他们的Teacher S,给他们出了一道作业题.Teacher S给了他们很多的点,让他们利用拉格朗日插值公式,计算出某严格单调函数的曲线.现在小白抄下了这些点,但是问题出现了,由于我们的小白同学上课时走了一下神,他多抄下来很多点,也就是说这些点整体连线不一定还是严格递增或递减的了.这可怎么处理呢.为此我们的小白同学制定了以下的取点规则: 1.取