POJ 2250 (LCS,经典输出LCS序列 dfs)
题目链接:
http://poj.org/problem?id=2250
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 9284 | Accepted: 3972 | Special Judge | ||
Description
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
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
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
#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)的更多相关文章
- POJ 2250 Compromise(LCS)
POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...
- 【POJ 2250】Compromise(最长公共子序列LCS)
题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...
- POJ 1159 回文串-LCS
题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- LCS以及输出路径模板
记忆 两个for用来寻找LCS,DP是二维的,每一维代表了字符串的长度. 寻找的代码部分 if(a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][ ...
- 拓扑排序详解(梅开二度之dfs版按字典序输出拓扑路径+dfs版输出全部拓扑路径
什么是拓扑排序? 先穿袜子再穿鞋,先当孙子再当爷.这就是拓扑排序! 拓扑排序说白了其实不太算是一种排序算法,但又像是一种排序(我是不是说了个废话qwq) 他其实是一个有向无环图(DAG, Direct ...
- 最长DNA重复序列长度,并输出该序列。 JAVA
1: 最长DNA重复序列长度,并输出该序列. 例如 ATCGTAGATCG,它的最大长度为4,序列为 ATCG. package com.li.huawei; import java.util.S ...
- POJ 2250 Compromise【LCS】+输出路径
题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...
- POJ - 2250 Compromise (LCS打印序列)
题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...
随机推荐
- 高级功能:很有用的javascript自定义事件
之前写了篇文章<原生javascript实现类似jquery on方法的行为监听>比较浅显,能够简单的使用场景. 这里的自定义事件指的是区别javascript默认的与DOM交互的事件,比 ...
- 区别String、StringBuilder、Stringbuffer的总结
1.三者在执行速度上: StringBuilder > StringBuffer > String 2. String:不可变长字符串 StringBuilder : 为可变长字符串 St ...
- Java并发编程指南
多线程是实现并发机制的一种有效手段.在 Java 中实现多线程有两种手段,一种是继承 Thread 类,另一种就是实现 Runnable/Callable 接口. java.util.concurre ...
- 安卓脱壳&&协议分析&&burp辅助分析插件编写
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 前言 本文以一个 app 为例,演示对 app脱壳,然后分析其 协 ...
- 【项目管理】git和码云的使用
缘起 说了那么多关于git和码云相关的事,一直都没给大伙讲解这个码云究竟是个啥玩意儿. 今天就给大伙说说如何通过git和码云搭建属于自己的代码库. 码云 码云(Git@OSC)是开源中国社区团队推出的 ...
- SQL Server ->> 查询添加XLOCK表提示不阻塞其他线程
BEGIN TRAN BEGIN END DROP TABLE IF EXISTS tempdb..#t CREATE TABLE #t ( spid int, dbid int, ObjId int ...
- iframe内联框
内联框中表格的下划线老是显示不出来,设置宽度百分比不起作用,调整了文本域的宽度也不行.只能动态调整iframe的高度.
- 转:未能打开编辑器:Unmatched braces in the pattern.
原文地址:http://blog.csdn.net/hytdsky/article/details/4736462 Eclipse出现这个问题而不能查看源代码 原因就是语言包的问题 出现这个问题了 ...
- pycharm something
e1:使用的版本2016.3 发现无法执行py程序,执行菜单为灰色.选择py为2.7. e2:使用过jython可以运行,但是语法有问题str.formate无法执行
- [翻译] About Core Image
About Core Image Core Image is an image processing and analysis technology designed to provide near ...