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. 关于javascript输出中文乱码的问题

    今天找到一个引导效果.原来是用英文进行引导.但是我改了里面的英文为汉字就出现乱码的情况.英文提示是在js页面里面完成的.所以最后的解决办法 就是把js文件用记事本打开,然后把文件另存为utf-8的格式 ...

  2. 我的css reset

    @charset "utf-8"; /*reset*/ body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,f ...

  3. dedecms自定义表单提交成功如何返回当前页面

    在plus/diy.php找到showmsg($bkmsg, $goto);改成showmsg($bkmsg, -1);

  4. CMD下查询Mysql中文乱码的解决方法

    我的MySQL是默认utf8编码的,所建数据库也是设置utf8编码,使用程序可以新增中文数据,在cmd中使用SQL语句新增数据则报类似Incorrect string value: '\xB2\xE2 ...

  5. opencv之图像膨胀

    #include <cv.h> #include <highgui.h> void main() { IplImage* src; IplImage*dst; src=cvLo ...

  6. opencv之图像腐蚀

    最近准备好好学习opencv 这博客就是我的学习笔记. #include <cv.h> #include <highgui.h> using namespace std; vo ...

  7. java学习之 垃圾回收

    垃圾回收器始终以一个较低优先级的后台进程进行垃圾的回收工作,这样不会影响程序的正常工作. 通常只有当内存到达用尽的边缘而程序需要分配新的内存空间时,垃圾回收器才会执行. 垃圾回收的条件:1,垃圾回收器 ...

  8. QLineEdit

    The QLineEdit widget is a one-line text editor. Header: #include <QLineEdit> qmake: QT += widg ...

  9. NandFlash驱动框架

    1.首先和前面的几个驱动程序相似,需要分配一个nand_chip结构体 s3c_nand = kzalloc(sizeof(struct nand_chip), GFP_KERNEL); 然后填充该结 ...

  10. CentOS 6.3 配置FTP

    一.FTP的安装 .检测是否安装了FTP:[root@localhost ~]# rpm -q vsftpd 如果安装了会显示版本信息: [root@localhost ~]# vsftpd-2.0. ...