题目链接:

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

分析:
经典的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==||j==)
return ;
if(x[i]==y[j])//来自左上角
{
dfs(i-,j-);
cout<<x[i]<<" ";//先递归到最后再输出,,这样就是顺序的
}
else
{
if(dp[i-][j]>dp[i][j-])//来自上面
{
dfs(i-,j);
}
else//来自左边
{
dfs(i,j-);
}
}
return ;
}
int main()
{
string s;
while(cin>>s)
{
l1=l2=;
if(s!="#")
{
x[++l1]=s;
while(cin>>s&&s!="#")
{
x[++l1]=s;
}
}
while(cin>>s&&s!="#")
{
y[++l2]=s;
}
memset(dp,,sizeof(dp));
for(int i=; i<=l1; i++)
{
for(int j=; j<=l2; j++)
{
if(x[i]==y[j])
{
dp[i][j]=dp[i-][j-]+;
}
else
{
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
}
dfs(l1,l2);
cout<<endl;
}
return ;
}

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. 2003 - Cann't connect to MySql server on - 'localhost'(10061)

    打开Navicat,打开连接失败,想必大家也会遇到这样的问题,错误消息提示如下: 解决方案如下:首先去看一下数据库服务是否开启,查看方式如下.1.打开任务管理器, oracle数据库服务 mysql数 ...

  2. 关于WebSocket协议

    WebSocket是单个TCP连接上进行全双工通信的协议 在WebSocket的API中,客户端与服务器只需要进行一次握手就可以保持持久的连接,并可以双向传输数据 与HTTP不同的是,WebSocke ...

  3. CSS 画一个八卦

    效果图: 实现原理: 设置高度为宽度的2倍的一个框,利用 border 补全另一半的宽度,设置圆角 用两个 div 设置不同的颜色,定位到圆的上下指定位置. 最后只剩下里面的小圆圈了.设个宽高,圆角即 ...

  4. 【vue】webpack插件svg-sprite-loader---实现自己的icon组件

    引言:最近开始写vue的项目,借鉴了一下vue-element-admin源码,针对vue有一个关于icon图标的处理,最近也找了很多关于vue的icon处理的解决方案,大部分都是按照之前小程序的方式 ...

  5. 那些年vue踩过的坑

    1.前言 学习Vue前端框架已经一个月了,作为一个web刚入门的菜鸟,在学习的过程中,网上有些技术博客往往没有什么可以借鉴的地方,在这里 我特意将我从开始一直到登录的过程记录下来.希望看到我的文章的朋 ...

  6. Linux+db2+was部署问题总结

     Linux+db2+was部署问题总结 前段日子在住建部进行了Linux环境下,db2+rbp+was的部署,由于是集群,切涉及到了很多was的东西,搞了很长时间,在此做一个问题总结,供后续查询 ...

  7. 继承ViewGroup学习onMeasure()和onLayout()方法

    在继承ViewGroup类时,需要重写两个方法,分别是onMeasure和onLayout. 1,在方法onMeasure中调用setMeasuredDimension方法void android.v ...

  8. Week3——文档代码分析

    该段代码代码显示了不使用异步处理的基本servlet: @WebServlet(urlPatterns={"/syncservlet"}) public class SyncSer ...

  9. LeetCode题解之Unique Morse Code Words

    1.题目描述 2.题目分析 将words 中的每一个string  直接翻译成对应的Morse 码,然后将其放入 set 中,最后返回set的大小即可,此处利用的set 中元素不重复的性质. 3.代码 ...

  10. Maven环境变量配置和在Eclipse中的配置

    1.Maven环境变量配置 M2_HOME :变量值为maven的安装目录 在path后添加%M2_HOME%\bin; 检查JDK,maven配置的cmd命令 echo %JAVA_HOME% ja ...