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. “-Xmx1024m -Xms1024m -Xmn512m -Xss256k”——Java运行参数(转)

    JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...

  2. 关注SSO

    https://wiki.jasig.org/display/CASC/Configuring+the+Jasig+CAS+Client+for+Java+in+the+web.xml 其余的看osc ...

  3. 微信开放框架-UCToo

    UCToo是一套简单,易用,开源的微信增值应用开发框架,帮助用户快捷的实现微信公众平台的个性化定制功能. http://www.uctoo.com/

  4. 黑马程序员_<<StringBuffer,包装类>>

    --------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1. StringBuffer 1.概述 S ...

  5. 删除input或textarea输入框在移动版显示的阴影(Safari/iPhone)

    移动端浏览网页元素时,自动给input和textarea添加了内部阴影的效果,只需加入样式即可去除 input,textarea{-webkit-appearance: none;} input[ty ...

  6. oralce11g 注冊表卸载20140810

    Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE] [-HKEY_LOCAL_MACHINE\SOFT ...

  7. Insert into a Cyclic Sorted List

    Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...

  8. 关于input标签的需要注意的几个小问题

    1.input元素没有结束标签,只有开始标签,即使写上结束标签也不起作用.如下 <input type="text" value="text" /> ...

  9. Java 比较两日期相差天数

    (版本1) publicstaticint getIntervalDays(Date fDate, Date oDate) { if (null == fDate || null == oDate) ...

  10. Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词

    #coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...