题目链接:

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. Django基础五之django模型层(一)单表操作

    一 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  2. github上的项目发布成静态网页

    代码上传成功之后就可以发布静态网页了,细心的童鞋应该已经看到我上传的代码在根目录就有一个html文件(发布其他情况没试过,感兴趣自己去尝试),发布的时候选择仓库即自动识别了. 第一步:点击settin ...

  3. 图片按钮(imageButton)

    图片按钮(imageButton) 常用属性: android:src="@drawable/download" (这里的download是一张图片的名称,通过引用该图片的名称直接 ...

  4. 这几个Xocode插件用过一段时间还比较稳定好用,Xcode6兼容,推荐给大家:

    这几个Xocode插件用过一段时间还比较稳定好用,Xcode6兼容,推荐给大家: AdjustFontSize: 快捷调整Xcode字体,https://github.com/zats/AdjustF ...

  5. Swagger RESTful API文档规范

    *注意编写的关键词:“必须”.“不能”.“需要”.“应当”,“不得”.“应该”.“不应该”,“推荐”.“可能”和“可选的” 原文链接:http://swagger.io/specification/ ...

  6. Linux 环境下为VirtualBox安装增强功能

    VirtualBox安装CentOS后,再安装增强功能就可以共享文件夹.粘贴板以及鼠标无缝移动,主要步骤如下: 1.yum -y update 2.yum -y install g++ gcc gcc ...

  7. Spring MVC 常用Jar包

    spring:http://maven.springframework.org/release/org/springframework/spring/ jackson:http://repo1.mav ...

  8. Breathing During Sleep

    TPO24-2 Breathing During Sleep Of all the physiological differences in human sleep compared with wak ...

  9. Asp.net MVC + Signalr 实现多人聊天室

    Asp.net SignalR 简介: 首先简单介绍一下Signalr ,我也是刚接触,觉得挺好玩的,然后写了一个多人聊天室. Asp.net SignalR 是为Asp.net 开发人员提供的一个库 ...

  10. JS获取长度方法总结

    目录: 1length 2size() 3length与size()的区别 4获取元素的索引 - index() 5获取对应的索引 - eq() 概述: 在工作中大家经常需要获取对象的长度,或者要获取 ...