题目链接:

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

die einkommen der landwirte
sind fuer die abgeordneten ein buch mit sieben siegeln
um dem abzuhelfen
muessen dringend alle subventionsgesetze verbessert werden
#
die steuern auf vermoegen und einkommen
sollten nach meinung der abgeordneten
nachdruecklich erhoben werden
dazu muessen die kontrollbefugnisse der finanzbehoerden
dringend verbessert werden
#

Sample Output

die einkommen der abgeordneten muessen dringend verbessert werden

Source

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

#include<cstring>
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#define max_v 1005
using namespace std;
string x[max_v],y[max_v];
int dp[max_v][max_v];
int l1,l2;
int dfs(int i,int j)
{if(i==0||j==0)return 0 ;if(x[i]==y[j])//来自左上角
    {dfs(i-1,j-1);cout<<x[i]<<" ";//先递归到最后再输出,,这样就是顺序的
    }else{if(dp[i-1][j]>dp[i][j-1])//来自上面
        {dfs(i-1,j);}else//来自左边
        {dfs(i,j-1);}}return 0;
}
int main()
{string s;while(cin>>s){l1=l2=0;if(s!="#"){x[++l1]=s;while(cin>>s&&s!="#"){x[++l1]=s;}}while(cin>>s&&s!="#"){y[++l2]=s;}memset(dp,0,sizeof(dp));for(int i=1; i<=l1; i++){for(int j=1; j<=l2; j++){if(x[i]==y[j]){dp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}}dfs(l1,l2);cout<<endl;}return 0;
}

转载于:https://www.cnblogs.com/yinbiao/p/9067766.html

POJ 2250 (LCS,经典输出LCS序列 dfs)相关推荐

  1. [算法]LCS及输出LCS

    最长公共子序列 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 #include<bits/stdc++. ...

  2. 第四周项目五-用递归方法求解(输出Fibnacci序列的第20个数)

    /* *Copyright(c)2016,烟台大学计算机与控制工程学院 *All rights reserved *文件名称:123.cpp *作 者:王蕊 *完成日期:2016年3月22日 *版 本 ...

  3. POJ 1159 - Palindrome 优化空间LCS

    将原串和其逆序串的最长公共子序列求出来为M..那么2*n-M就是所需要加的最少字符..因为求出的M就是指的原串中"潜伏"的最长回文.. 问题转化为求LCS..但是n最大到5000. ...

  4. 【POJ - 2676】Sudoku (经典深搜,dfs数独)

    题干: Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller s ...

  5. POJ 1458 Common Subsequence DP LCS 最长公共子序列

    最长公共子序列,照抄<算法设计与分析导论>P138-140 设输入的两个字符串分别为a1,a2,```,am(串a) b1,b2,````,bn(串b) 设d(i,j)为字符串a1,a2, ...

  6. 动态规划---实现输出最大公共子序列的长度以及输出最大子字符串(java语言描述)

    参考博客地址:http://blog.csdn.NET/biangren/article/details/8038605 http://blog.csdn.net/njr465167967/artic ...

  7. POJ 1088 滑雪(输出对比)

    http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 68159 ...

  8. c语言输出法雷序列,法雷(法里)序列 - osc_h0wb1wlt的个人空间 - OSCHINA - 中文开源技术交流社区...

    定义: 对任意给定的一个自然数n,将分母小于等于n的不可约的真分数按升序排列,并且在第一个分数之前加上0/1,在最后一个分数之后加上1/1,这个序列称为n级法雷数列,即法雷数列是0和1之间最简分数升序 ...

  9. POJ 1691 - Painting A Board + Python (DFS)

    题目链接:1691 -- Painting A Board 参考资料:POJ 1691 - Painting A Board | 眈眈探求 特别注意:注意本文的升序排列语句. 一 题目描述: 给定一个 ...

最新文章

  1. 为什么使用RLC表测量电感在不同的频率测量值不同呢?
  2. 前端培训_backbone
  3. ngnix服务器搭建
  4. 使用 Linux 系统调用的内核命令
  5. 教你如何用Python追踪快递信息!
  6. strcmp() Anyone? UVA - 11732 左孩子右兄弟Trie/计数
  7. python sklearn: 模型(如 SVM,PCA等)的保存与加载调用
  8. 人情味,让你的内容脱颖而出
  9. 在主函数中输入10个等长的字符串。用另一函数对他们排序
  10. sqlserver2000中字符串类型的日期如何比较大小
  11. ps,ae,ui,ai,pr,cad,3DMAX,c4d,cdr,摄影后期
  12. Qt界面显示OpenCV读取的图片
  13. Oblivious transfer and Garbled circuits
  14. Ubuntu各个版本下载和安装
  15. Windows系统文件详解
  16. 自制一个交叉适配器来检修网络设备(转)
  17. 【Vue基础】关于Vue中CSS的scoped属性作用域与样式穿透
  18. 公众号 自动生成海报 python_Python 生成公众号头图 1.0
  19. 液晶屏背光板的分类及知识点
  20. 什么是数字化?为什么需要数字化?

热门文章

  1. linux常用命令汇总(pwd,echo,history,nano)
  2. 将多张图整合到一张大图中,再用css定位技术
  3. GPL与LGPL的区别
  4. 让人郁闷的“DesktopCompatible”
  5. 开发直播APP选择云服务器的优点
  6. mapPartition方法与map方法的区别(转载)
  7. flink on yarn shell的session cluster模式实验记录
  8. 关于yarn.nodemanager.vmem-pmem-ratio的通俗解释
  9. intellij2018修改代码背景颜色
  10. 图像种类的基本概念整理