Compromise 

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 Specification

The input file 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 Specification

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问题。要打印路径。可以用一个VIS数组在过程中记录下状态转移方式。最后在根据这去遍历输出。

代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h> char a[105][35], b[105][35], c, cc, flag;
int dp[105][105], vis[105][105], aa, bb, aaa , bbb, i, j; void print(int i, int j) {
if (!i || !j)
return;
if (vis[i][j] == 1) {
print(i - 1, j - 1);
if (flag)
printf(" ");
else
flag = 1;
printf("%s", a[i - 1]);
}
else if(vis[i][j] == 0)
print(i - 1, j);
else
print(i, j - 1);
}
int main() {
flag = aa = bb = aaa = bbb = 0;
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
while (~scanf("%s", a[aa])) {
if (a[aa][0] == '#') {
while (~scanf("%s", b[bb]) && b[bb][0] != '#')
bb ++;
for (i = 1; i <= aa; i ++)
for (j = 1; j <= bb; j ++) {
if (strcmp(a[i - 1], b[j - 1]) == 0) {
dp[i][j] = dp[i - 1][j - 1] + 1;
vis[i][j] = 1;
}
else {
if (dp[i - 1][j] > dp[i][j - 1]) {
dp[i][j] = dp[i - 1][j];
vis[i][j] = 0;
}
else {
dp[i][j] = dp[i][j - 1];
vis[i][j] = -1;
}
}
}
print(aa, bb);
printf("\n");
flag = aa = bb = aaa = bbb = 0;
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
continue;
}
aa ++;
}
return 0;
}

UVA 531 - Compromise(dp + LCS打印路径)的更多相关文章

  1. UVA 1626 区间dp、打印路径

    uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...

  2. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

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

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

  4. DP(递归打印路径) UVA 662 Fast Food

    题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + ...

  5. 最长公共子序列Lcs(打印路径)

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的).   比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...

  6. UVA 624 (0 1背包 + 打印路径)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...

  7. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...

  8. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  9. POJ2250 - Compromise(LCS+打印路径)

    题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...

随机推荐

  1. Oracle Primavera P6 R84单机版安装教程

    本教程用于指导Oracle Primavera P6 R84版本的单机版安装过程.P6 R84版本于2014年9月30日发布.其功能较之前版本有较大提升.单机版的安装方式也发生了很大的变化.P6 R8 ...

  2. javascript收集整理

    //特殊字符去掉 function check(obj){ var test=obj.value.match(/[^A-Za-z0-9]/g);//这条语句表示在输入文本中是否包含不符合要求的字符,如 ...

  3. DropDownList控件学习

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. mahout贝叶斯算法开发思路(拓展篇)2

    如果想直接下面算法调用包,可以直接在mahout贝叶斯算法拓展下载,该算法调用的方式如下: $HADOOP_HOME/bin hadoop jar mahout.jar mahout.fansy.ba ...

  5. WinRT Toolkit 介绍--Control篇

    WinRT toolkit是组针对Windows Runtime XAML开发的一系列Control,extension和helper类.它和Windows Phone Toolkit一样,也是由很多 ...

  6. openstack之Glance

    一.Glance简介.基本概念: Glance是openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等操作. Glance提供restful API可以查询虚拟机镜像的m ...

  7. 加特殊符号星号斜杠反斜杠/* \ */ !important等让css实现兼容各个浏览器的技巧的代码

       在编写css样式表的时候常常会碰到一写浏览器兼容的问题,象是不同内核的浏览器显示就不一定相同,不同版本的的浏览器也会产生上下兼容的问题,如何解决这些问题成了我们苦恼的问题,如果你对css hac ...

  8. tomcat部署web项目的三种方式

    方式一:将web项目拷贝至webapps目录下. 方式二:修改tomcat目录下的conf目录下的server.xml,在其<Host>标签中添加子标签,代码如下: <Host ap ...

  9. packstack安装以及centos源配置注意事项

    On CentOS:安装分为四步: 1,$ sudo yum install -y centos-release-openstack-mitaka 2,$ sudo yum update -y 3,$ ...

  10. 使用python操作RabbitMQ,Redis,Memcache,SQLAlchemy 其二

    一.概念 1.Memcached     Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态 ...