compromise

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

 

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

题解:

题意:第一眼看,输入的东西有点长啊,仔细读了读,发现就是找到一个最长公共子序列并打印。

分析:找到最长公共子序列不难,难的是打印它的一条路径。我们知道是用二维数组dp来保存它的匹配数。一旦有匹配的它就会修改后面的值,保证了

如何一个状态当前的dp数据中是最大的值。为了记录它路径。我们需要用数组t来记录。可以用递归来实现。

可能这样说太抽象了,附上一张二维图。

ABCBDAB

BDCABA

两个序列,求最长公共子序列。图中就是路径回溯,这就是选择的过程,看懂了它再去看代码吧

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t[][],dp[][];
string a[],b[];
void lcs(int x,int y)
{
for(int i=; i<=x; i++)
for(int j=; j<=y; j++)
{
if(a[i]==b[j])
{
dp[i][j]=dp[i-][j-]+;
t[i][j]=;
}
else
{
if(dp[i][j-]<=dp[i-][j])
{
dp[i][j]=dp[i-][j];
t[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
t[i][j]=;
}
}
}
} void output(int x,int y)
{
if(x==||y==) return;
if(t[x][y]==)
{
output(x-,y-);
cout<<a[x]<<" ";
}
else if(t[x][y]==)
output(x-,y);
else if(t[x][y]==)
output(x,y-);
} int main()
{
string s;
while(cin>>s)
{
int m=,n=;
a[]=s;
while(cin>>s&&s!="#")
{
a[m++]=s;
}
while(cin>>s&&s!="#")
{
b[n++]=s;
}
lcs(m,n);
output(m,n);
cout<<endl;
}
return ;
}

POJ 2250(LCS最长公共子序列)的更多相关文章

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

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

  2. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  3. 动态规划模板2|LCS最长公共子序列

    LCS最长公共子序列 模板代码: #include <iostream> #include <string.h> #include <string> using n ...

  4. LCS 最长公共子序列

    区别最长公共子串(连续) ''' LCS 最长公共子序列 ''' def LCS_len(x, y): m = len(x) n = len(y) dp = [[0] * (n + 1) for i ...

  5. POJ 1159 Palindrome(最长公共子序列)

    Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...

  6. LCS最长公共子序列(最优线性时间O(n))

    这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...

  7. LCS最长公共子序列

    问题:最长公共子序列不要求所求得的字符串在所给字符串中是连续的,如输入两个字符串ABCBDAB和BDCABA,字符串BCBA和BDAB都是他们的公共最长子序列 该问题属于动态规划问题 解答:设序列X= ...

  8. LCS最长公共子序列HDU1159

    最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...

  9. LCS最长公共子序列~dp学习~4

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...

随机推荐

  1. [jAudio] JAVA上经典特征提取工具

    1.下载Jar包后引入 现在的问题是jAudio通常是给人UI进行操作的,直接使用怎么办? 看了源码,发现特征提取是通过类之间交叉调用实现的,是否有办法整合一下?

  2. 【模拟】Class 解题报告

    [问题描述] 信息班这期的课将要结束了,老师要从现在班上的同学中选出比较优秀的同学进入下一期的学习.而录取标准则是将平时作业和考试一起考虑,综合成绩排在前面的则录取.经过一番思考,老师作了以下的筛选计 ...

  3. 使用Ant自动化发布web工程

    通常在web应用程序需要上线或测试时通常需要部署到类似于tomcat.jboss.weblogic或webspare这些web服务器中,为避免手动部署带来的操作繁琐.易出错等问题,这里使用ant进行标 ...

  4. UIActionSheet底部弹出框

    <底部弹出框来提示用户信息>    1.遵循代理方法<UIActionSheetDelete>    2.调用放法 [UIActionSheet *sheet=[UIActio ...

  5. android如何建立数据库。(如何重写SQLiteOpenHelper)

    public class DBConnection extendsSQLiteOpenHelper{//继承SQLiteOpenHelper, public DBConnection(Context ...

  6. Oracle MERGE INTO的使用方法

    非常多时候我们会出现例如以下情境,假设一条数据在表中已经存在,对其做update,假设不存在,将新的数据插入.假设不使用Oracle提供的merge语法的话,可能先要上数据库select查询一下看是否 ...

  7. [io PWA] keynote: Launching a Progressive Web App on Google.com

    Mainly about Material design (effects / colors / flashy stuff) Components (web components / polymer) ...

  8. [PWA] 16. Clean IDB

    When we save items to IndexDB, we need to think about clean the items or keep those in a short list. ...

  9. 《Android开发艺术探索》读书笔记 (11) 第11章 Android的线程和线程池

    第11章 Android的线程和线程池 11.1 主线程和子线程 (1)在Java中默认情况下一个进程只有一个线程,也就是主线程,其他线程都是子线程,也叫工作线程.Android中的主线程主要处理和界 ...

  10. Python之路,Day17 - 分分钟做个BBS论坛

    Python之路,Day17 - 分分钟做个BBS论坛   本节内容: 项目:开发一个简单的BBS论坛 需求: 整体参考"抽屉新热榜" + "虎嗅网" 实现不同 ...