POJ 2250 Compromise(LCS)
POJ 2250 Compromise(LCS)解题报告
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D
题目:
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
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
题目大意:
给出两篇文章,文章有单词构成,每个单词由空格隔开,文章以#结束,输出两篇文章的公共单词串。 分析:
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)的更多相关文章
- POJ 2250 (LCS,经典输出LCS序列 dfs)
题目链接: http://poj.org/problem?id=2250 Compromise Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- LCS(打印路径) POJ 2250 Compromise
题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...
- POJ 2250 Compromise【LCS】+输出路径
题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...
- POJ - 2250 Compromise (LCS打印序列)
题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...
- poj 2250 Compromise(区间dp)
题目链接:http://poj.org/problem?id=2250 思路分析:最长公共子序列问题的变形,只是把字符变成了字符串,按照最长公共子序列的思路即可以求解. 代码如下: #include ...
- POJ 2250 Compromise (UVA 531)
LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...
- 【POJ 2250】Compromise(最长公共子序列LCS)
题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...
- POJ 2250(LCS最长公共子序列)
compromise Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- POJ2250:Compromise(LCS)
Description In a few months the European Currency Union will become a reality. However, to join the ...
随机推荐
- SQL Server 储存过程的output 参数
要做的参数的回传一方面要做到有储存过程的配合,再一方面也要有调用方法的配合,也就是说错误的调用方法是没有办法把值回传的. 下面是例子 --1.储存过程方面的配合 create procedure db ...
- 关注SSO
https://wiki.jasig.org/display/CASC/Configuring+the+Jasig+CAS+Client+for+Java+in+the+web.xml 其余的看osc ...
- delphi datasnap 心跳包
为了能让我们的服务程序更加稳定,有些细节问题必须解决.就如上一讲中提到的客户端拔掉网线,造成服务器上TCP变成死连接,如果死连接数量过多,对服务器能长期稳定运行是一个巨大的威胁.另外,经过测试,如果服 ...
- 简单makefile的写法
一个项目下的文件比较多,如果单个的输入,比较费劲,所以就需要把编译过程写进一个MakeFile文件中. 下面建立5个文件,3个cxx文件,2个hxx头文件 //filename main.cxx #i ...
- SSO 基于Cookie+fliter实现单点登录(SSO):工作原理
SSO的概念: 单点登录SSO(Single Sign-On)是身份管理中的一部分. SSO的一种较为通俗的定义是:SSO是指訪问同一server不同应用中的受保护资源的同一用户,仅仅须要登录一次,即 ...
- hdu2554-N对数的排列问题
http://acm.hdu.edu.cn/showproblem.php?pid=2554 假设所有的2n个数据的位置分别从1~2n标号. 现在假设其中第ai个数据(双胞胎),和bi.那么他们的位置 ...
- 传统web和mvc的区别
- ShineTime 是一个效果非常精致的缩略图相册
ShineTime 是一个效果非常精致的缩略图相册,鼠标悬停到缩略图的时候有很炫的闪光效果,基于 CSS3 实现,另外缩略图也会有立体移动的效果.特别适用于个人摄影作品,公司产品展示等用途,快来来围观 ...
- hough变换中,直线方程从XY空间转换到参数空间的转换过程
XY空间直线方程:y=kx+b 参数空间直线方程:xcosθ+ysinθ=ρ 直线方程从XY空间转换到参数空间过程的转换过程: k=tan(π-α)=tan(-α)=-tanα=-cotθ=-cosθ ...
- Java RMI 简介及其优劣势总结
今天,帮别人看UDAS配置问题时,看到采用的是RMI方式的调用,如:rmi://10.20.134.140:2299/DataServer,而且这个端口是被直接硬编码在UDAS的代码中的.既然看到了, ...