题目链接:

http://poj.org/problem?id=2250

Compromise
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9284   Accepted: 3972   Special Judge

Description

In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do.

Therefore the German government requires a program for the following task:

Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind).

Your country needs this program, so your job is to write it for us.

Input

The input will contain several test cases.

Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'.

Input is terminated by end of file.

Output

For each test case, print the longest common subsequence of words occuring in the two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.

Sample Input

  1. die einkommen der landwirte
  2. sind fuer die abgeordneten ein buch mit sieben siegeln
  3. um dem abzuhelfen
  4. muessen dringend alle subventionsgesetze verbessert werden
  5. #
  6. die steuern auf vermoegen und einkommen
  7. sollten nach meinung der abgeordneten
  8. nachdruecklich erhoben werden
  9. dazu muessen die kontrollbefugnisse der finanzbehoerden
  10. dringend verbessert werden
  11. #

Sample Output

  1. die einkommen der abgeordneten muessen dringend verbessert werden

Source

分析:
经典的LCS问题,重点是采用DFS输出此LCS序列中的一个
注意输入两个序列的方式,学习了
注意输入序列的下标从1开始
注意DFS要先递归到最后再输出序列
注意dp的初始化,直接memset就可以
代码如下:
 
代码如下;

  1. #include<cstring>
  2. #include<cstdio>
  3. #include<string>
  4. #include<iostream>
  5. #include<algorithm>
  6. #define max_v 1005
  7. using namespace std;
  8. string x[max_v],y[max_v];
  9. int dp[max_v][max_v];
  10. int l1,l2;
  11. int dfs(int i,int j)
  12. {
  13. if(i==||j==)
  14. return ;
  15. if(x[i]==y[j])//来自左上角
  16. {
  17. dfs(i-,j-);
  18. cout<<x[i]<<" ";//先递归到最后再输出,,这样就是顺序的
  19. }
  20. else
  21. {
  22. if(dp[i-][j]>dp[i][j-])//来自上面
  23. {
  24. dfs(i-,j);
  25. }
  26. else//来自左边
  27. {
  28. dfs(i,j-);
  29. }
  30. }
  31. return ;
  32. }
  33. int main()
  34. {
  35. string s;
  36. while(cin>>s)
  37. {
  38. l1=l2=;
  39. if(s!="#")
  40. {
  41. x[++l1]=s;
  42. while(cin>>s&&s!="#")
  43. {
  44. x[++l1]=s;
  45. }
  46. }
  47. while(cin>>s&&s!="#")
  48. {
  49. y[++l2]=s;
  50. }
  51. memset(dp,,sizeof(dp));
  52. for(int i=; i<=l1; i++)
  53. {
  54. for(int j=; j<=l2; j++)
  55. {
  56. if(x[i]==y[j])
  57. {
  58. dp[i][j]=dp[i-][j-]+;
  59. }
  60. else
  61. {
  62. dp[i][j]=max(dp[i-][j],dp[i][j-]);
  63. }
  64. }
  65. }
  66. dfs(l1,l2);
  67. cout<<endl;
  68. }
  69. return ;
  70. }

POJ 2250 (LCS,经典输出LCS序列 dfs)的更多相关文章

  1. POJ 2250 Compromise(LCS)

    POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...

  2. 【POJ 2250】Compromise(最长公共子序列LCS)

    题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...

  3. POJ 1159 回文串-LCS

    题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...

  4. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  5. LCS以及输出路径模板

    记忆 两个for用来寻找LCS,DP是二维的,每一维代表了字符串的长度. 寻找的代码部分 if(a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][ ...

  6. 拓扑排序详解(梅开二度之dfs版按字典序输出拓扑路径+dfs版输出全部拓扑路径

    什么是拓扑排序? 先穿袜子再穿鞋,先当孙子再当爷.这就是拓扑排序! 拓扑排序说白了其实不太算是一种排序算法,但又像是一种排序(我是不是说了个废话qwq) 他其实是一个有向无环图(DAG, Direct ...

  7. 最长DNA重复序列长度,并输出该序列。 JAVA

    1:  最长DNA重复序列长度,并输出该序列. 例如  ATCGTAGATCG,它的最大长度为4,序列为 ATCG. package com.li.huawei; import java.util.S ...

  8. POJ 2250 Compromise【LCS】+输出路径

    题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...

  9. POJ - 2250 Compromise (LCS打印序列)

    题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...

随机推荐

  1. AE中IHookHelper的用法 来自http://blog.sina.com.cn/s/blog_6faf711d0100xs1x.html

    IHookHelper 主要在用在自定义类型于AE带的的ICommand或ITool等, 1.实例化IHookHelper 对象: IHookHelper m_hookHelper = new Hoo ...

  2. Attack Top Chunk之 bcloud

    前言 这是 bctf 2016 的题,链接 https://github.com/ctfs/write-ups-2016/tree/master/bctf-2016/exploit/bcloud-20 ...

  3. Activity被回收导致fragment的getActivity为空

    在编写含有Fragment代码的时候,经常会遇到这种情况,假如app长时间在后台运行,再点击进入会crash,而且fragment页面有重叠的现象. 如果系统内存不足.或者切换横竖屏.或者app长时间 ...

  4. 团队项目个人进展——Day01

    一.昨天工作总结 冲刺第一天,昨天阅读了小程序官方文档关于对视图层和逻辑层的介绍 二.遇到的问题 对小程序的样式文件——WXML里的标签不太理解,相比之下,html的标签更能让人接受 三.今日工作规划 ...

  5. 'webpack' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    npm updatea -g 很严重,把本地npm安装包都更新了,跟项目npm安装包版本不一,导致 意思是版本冲突,手动卸载了,重新安装还是最新版本,很是头疼.找同事的电脑拷贝了一份,然后复制过来报“ ...

  6. 探讨Oracle分区表

    一年又一年,又到年底了,对于数据库的分区表需要检查一下,有无最大分区,次分区是否需要追加分区,如果程序不是自动追加分区的话,那么年中结算的时候,就会报错. 1.oracle分区主要有五种类型 (1)R ...

  7. java多线程读取、操作List集合

    import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ArrayUtils;   pub ...

  8. C语言高精度乘法

    #include <stdio.h> void highPrecision (int N ); ] = {, }, length = ; //开辟一个大的数组,全局变量length记录长度 ...

  9. 安装Window Server 2008的些配置

    上次安装window server2008,由于server2008需要设置很多东西,不然用起来很不爽,就说IE吧,每次随便打开一个网页都要弹出n多窗口出来叫你添加到信任域里面!太烦人了![下面有解决 ...

  10. 【matlab】 幂法 求解最大特征值

    一. 算法: 1.输入矩阵A,初始向量x误差限ep,最大迭代次数N 2.置 k = 1, m1 = 0; 3.求Xr-> norm(x)   abs(Xr)=max[Xi] 1<=i< ...