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. 各种排序算法(C语言)

    #include <stdlib.h> #include <stdio.h> void DataSwap(int* data1, int* data2) { int temp ...

  2. 什么是epoll

    什么是epoll epoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.当然,这不是2.6内核才有的,它是在2.5.44内核中被引进的(epoll(4) is a new A ...

  3. sql的强大功能(看一条sql解决的复杂业务)

        一条sql语句解决的复杂业务,请往下看:     业务介绍:一个单位有多个立项(立项信息表里有单位id),每个立项可能被预警多次(预警信息表里的uuid字段的值里包含有立项id或单位id),每 ...

  4. JavaScript中你可能不知道的九件事

    今天凑巧去W3School扫了一遍JavaScript教程,发现从中看到了不少自己曾经没有注意过的细节. 我这些细节列在这里.分享给可能相同不知道的朋友: 1.使用 document.write() ...

  5. android 视频播放器的INTENT-FILTER属性

    <intent-filter>                   <action android:name="android.intent.action.VIEW&quo ...

  6. Windows API中几个函数的总结

    [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr ...

  7. CSS3滤镜

    今天在办公室亲眼目睹了同事使用CSS3滤镜为一张漂亮的照片轮廓加上了阴影,瞬间亮瞎了我的的双眼,见笑了. 所以也迅速尝试使用CSS3滤镜让最新出炉的MUI LOGO也性感一把,试图来愉悦一下大家的双眼 ...

  8. ZOJ 2770 Burn the Linked Camp(spfa&&bellman)

    //差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...

  9. hough变换是如何检测出直线和圆的?

    (I)直线篇 1 直线是如何表示的?对于平面中的一条直线,在笛卡尔坐标系中,常见的有点斜式,两点式两种表示方法.然而在hough变换中,考虑的是另外一种表示方式:使用(r,theta)来表示一条直线. ...

  10. VS2013 RC 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral.........

    微软发布了vs2013的RC版本,更新了自己机器上的vs,在创建项目过程中,发现出现如题的相关错误,查了相关msdn的资料,才了解到vs已经全面切换到使用NuGet这个第三方开源工具来管理项目包和引用 ...