POJ 2250 Compromise(LCS)解题报告

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D

题目:

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<cstdio>
#include<iostream>
#include<cstring>
using namespace std; char a[][],b[][],c[][];
int dp[][],mark[][],l1,l2,cnt; void LCS()
{
int i,j;
memset(dp,,sizeof(dp));
memset(mark,,sizeof(mark));
for(i=;i<=l1;i++)
mark[i][]=;
for(i=;i<=l2;i++)
mark[][i]=-;
for(i=;i<=l1;i++)
{
for(j=;j<=l2;j++)
{
if(!strcmp(a[i-],b[j-]))
{
dp[i][j]=dp[i-][j-]+;
mark[i][j]=;
}
else if(dp[i-][j]>=dp[i][j-])
{
dp[i][j]=dp[i-][j];
mark[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
mark[i][j]=-;
}
}
}
} void print(int i,int j)
{
if(!i&&!j)
return;
if(mark[i][j]==)
{
print(i-,j-);
strcpy(c[cnt++],a[i-]);
}
else if(mark[i][j]==)
print(i-,j);
else
print(i,j-);
} int main()
{
while(scanf("%s",a[])!=EOF)
{
l1=;
while(strcmp(a[l1-],"#"))
scanf("%s",a[l1++]);
l1=l1-;
scanf("%s",b[]);
l2=;
while(strcmp(b[l2-],"#"))
scanf("%s",b[l2++]);
LCS();
cnt=;
print(l1,l2);
printf("%s",c[]);
for(int i=;i<cnt;i++)
printf(" %s",c[i]);
printf("\n");
}
return ;
}


POJ 2250 Compromise(LCS)的更多相关文章

  1. POJ 2250 (LCS,经典输出LCS序列 dfs)

    题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  2. LCS(打印路径) POJ 2250 Compromise

    题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...

  3. POJ 2250 Compromise【LCS】+输出路径

    题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...

  4. POJ - 2250 Compromise (LCS打印序列)

    题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...

  5. poj 2250 Compromise(区间dp)

    题目链接:http://poj.org/problem?id=2250 思路分析:最长公共子序列问题的变形,只是把字符变成了字符串,按照最长公共子序列的思路即可以求解. 代码如下: #include ...

  6. POJ 2250 Compromise (UVA 531)

    LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...

  7. 【POJ 2250】Compromise(最长公共子序列LCS)

    题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...

  8. POJ 2250(LCS最长公共子序列)

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

  9. POJ2250:Compromise(LCS)

    Description In a few months the European Currency Union will become a reality. However, to join the ...

随机推荐

  1. Linux ---> 监控JVM工具

    Linux ---> 监控JVM工具shkingshking 发布时间: 2013/10/10 01:27 阅读: 2642 收藏: 26 点赞: 1 评论: 0 JDK内置工具使用 jps(J ...

  2. CSSBox - Java HTML rendering engine

    CSSBox - Java HTML rendering engine CSSBox is an (X)HTML/CSS rendering engine written in pure Java. ...

  3. [置顶] 【GBT28181开发:SIP协议实践】之设备远程启动

    下面学习的是设备远程控制的流程,和设备信息.设备目录.设备状态的流程差不多,主要是描述的协议字段不同,模拟SPVMN系统向源设备发送远程启动控制指令,记录下交互的消息,详细研究了下: 转载请注明出处: ...

  4. 【Oracle】RAC添加新节点

    RAC添加节点: 环境: OS:OEL5.6 RAC:10.2.0.1.0 原有rac1,rac2两个节点.如今要添加rac3节点: 操作过程: 改动三个节点上的/etc/hosts文件 192.16 ...

  5. ios蓝牙开发(一)蓝牙相关基础知识

    蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...

  6. Cocos2d-X 动作展示《一》

    因为Cocos2d-X中的动作较多,我将全部的动作制作成了一个滚动视图.每一个滚动视图上都有动作名,单击滚动视图就能够展示对应的动作 程序效果图: 使用滚动视图实现动作切换 动作展示 程序代码: 首先 ...

  7. 如何得到Sessionid的值

    当用户向一个网站请求第一个页面时,用户会话启动.当第一个页面被请求时,web服务器将asp.net_sessionID  cookie添加进用户的浏览器.可以使用newsession属性探测新会话的启 ...

  8. 读取excel出现空值

    表格里某列假如混杂了数字和字符就会有可能部分读取为空,连接Excel方法如下 string strConn = 'Provider=Microsoft.Jet.OLEDB.4.0;' 'Data So ...

  9. zoj 2229 Ride to School

    所有车子到达的总时间算出来,然后从小到大排序,如果:1. 开始时间 < 0 的,不予考虑,太快的赶不上,太慢的赶上也没用.2. 开始时间 > 0 的,Charley 和最早到达的车子一起到 ...

  10. 初步认识ExtJS

    最近因为项目,需要去学习ExtJS的相关内容. 现在对于ExtJS完全是小白一枚. 目前使用的是ExtJS4.2的版本,官网上现在最新版本是6的. 第一个方法:Ext.onReady() Ext.on ...