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
题意:给出两段文字,求出最长的公共单词串
思路:LCS问题,只需要开个二维来记录就好了
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; char a[35][105],b[35][105],c[35][105];
int dp[105][105],mark[105][105],len1,len2,cnt; void LCS()
{
int i,j;
memset(dp,0,sizeof(dp));
memset(mark,0,sizeof(mark));
for(i = 0;i<=len1;i++)
mark[i][0] = 1;
for(i = 0;i<=len2;i++)
mark[0][i] = -1;
for(i = 1; i<=len1; i++)
{
for(j = 1; j<=len2; j++)
{
if(!strcmp(a[i-1],b[j-1]))
{
dp[i][j] = dp[i-1][j-1]+1;
mark[i][j] = 0;
}
else if(dp[i-1][j]>=dp[i][j-1])
{
dp[i][j] = dp[i-1][j];
mark[i][j] = 1;
}
else
{
dp[i][j] = dp[i][j-1];
mark[i][j] = -1;
}
}
}
} void PrintLCS(int i,int j)
{
if(!i&&!j)
return ;
if(mark[i][j]==0)
{
PrintLCS(i-1,j-1);
strcpy(c[cnt++],a[i-1]);
}
else if(mark[i][j]==1)
{
PrintLCS(i-1,j);
}
else
{
PrintLCS(i,j-1);
}
} int main()
{
int i;
while(~scanf("%s",a[0]))
{
len1 = 1;
while(strcmp(a[len1-1],"#"))
scanf("%s",a[len1++]);
len1-=1;
scanf("%s",b[0]);
len2 = 1;
while(strcmp(b[len2-1],"#"))
scanf("%s",b[len2++]);
LCS();
cnt = 0;
PrintLCS(len1,len2);
printf("%s",c[0]);
for(i = 1; i<cnt; i++)
{
printf(" %s",c[i]);
}
printf("\n");
} return 0;
}

POJ2250:Compromise(LCS)的更多相关文章

  1. POJ 2250 Compromise(LCS)

    POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...

  2. POJ2250 - Compromise(LCS+打印路径)

    题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...

  3. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  4. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  5. 51nod 1006 最长公共子序列Lcs(经典动态规划)

    传送门 Description 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是 ...

  6. 整理的一些模版LCS(连续和非连续)

    对于连续的最大串,我们称之为子串....非连续的称之为公共序列.. 代码: 非连续连续 int LCS(char a[],char b[],char sav[]){ int lena=strlen(a ...

  7. 算法导论-动态规划(最长公共子序列问题LCS)-C++实现

    首先定义一个给定序列的子序列,就是将给定序列中零个或多个元素去掉之后得到的结果,其形式化定义如下:给定一个序列X = <x1,x2 ,..., xm>,另一个序列Z =<z1,z2  ...

  8. 最长公共子序列(LCS问题)

    先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...

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

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

随机推荐

  1. js 表达式与运算符 详解(下)

    比较运算符: > .>= .<. <=.  ==. !=. ===. !==. 比较运算符的结果都为布尔值 ==只比较值是否相等    而    ===比较的是值和数据类型都要 ...

  2. 关于本地计算机无法启动Apache2

    最近因工作需要,要学习PHP的基础编程,于是学习架设PHP工作环境. 但按照教材上介绍的那样,安装了WMAP后,一直无法运行成功.后发现Apache一直都不在运行状态.到WMAP中的Apache选项中 ...

  3. block 数组排序

    #import <Foundation/Foundation.h> //定义⼀一个block,返回值为BOOL,有两个NSString参数.实现:判 //断字符串是否相等. BOOL (^ ...

  4. Unity问答——怎么知道屏幕中目前有多少个敌人?

    这篇博客源自我在泰课在线的回答.链接:http://www.taikr.com/group/1/thread/92 问:怎么知道屏幕中目前有多少个敌人? 答: 思路一:仅适用于2D游戏,因为这个方法没 ...

  5. Xamarin Mono错误: unable to find explicit activity class

    unable to find explicit activity class在android开发很常见,网上一般是java的解决办法,对我们这些xamariner就无语了. xamarin中用attr ...

  6. BZOJ 3223 文艺平衡树

    Description   您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2  ...

  7. TWinControl的刷新过程(5个非虚函数,4个覆盖函数,1个消息函数,默认没有双缓冲,注意区分是TCustomControl还是Windows原生封装控件,执行流程不一样)

    前提条件:要明白在TWinControl有以下四个函数的存在,注意都是虚函数: procedure Invalidate; override;procedure Update; override;pr ...

  8. Maven实战六

    转载:http://www.iteye.com/topic/1132509 一.简介 settings.xml对于maven来说相当于全局性的配置,用于所有的项目,当Maven运行过程中的各种配置,例 ...

  9. node.js基础模块http、网页分析工具cherrio实现爬虫

    node.js基础模块http.网页分析工具cherrio实现爬虫 一.前言      说是爬虫初探,其实并没有用到爬虫相关第三方类库,主要用了node.js基础模块http.网页分析工具cherri ...

  10. Pie(二分)

    ime Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8930   Accepted: 3235   Special Judge De ...