UVA 531 - Compromise(dp + LCS打印路径)
| 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打印路径)的更多相关文章
- UVA 1626 区间dp、打印路径
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- LCS(打印路径) POJ 2250 Compromise
题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...
- DP(递归打印路径) UVA 662 Fast Food
题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + ...
- 最长公共子序列Lcs(打印路径)
给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...
- UVA 624 (0 1背包 + 打印路径)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...
- FatMouse's Speed ~(基础DP)打印路径的上升子序列
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- POJ2250 - Compromise(LCS+打印路径)
题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...
随机推荐
- 射频识别技术漫谈(11)——Mifare系列卡的共性
Mifare是NXP公司生产的一系列遵守ISO14443A标准的射频卡,包Mifare S50.Mifare S70.Mifare UltraLight.Mifare Pro.Mifare Desfi ...
- 简识UML语言(转)
在学习过程中用到了一些框图,用于绘画框图的语言数不胜数,本篇将就学习中用的一款绘画框图的语言做个简单的介绍. 直奔主题,采用一种可视化的面向对象的建模语言---UML,UML使用一些标准的圆形元素直观 ...
- BZOJ 1874 取石子游戏 (NIM游戏)
题解:简单的NIM游戏,直接计算SG函数,至于找先手策略则按字典序异或掉,去除石子后再异或判断,若可行则直接输出. #include <cstdio> const int N=1005; ...
- ubuntu安装hadoop 若干问题的解决
问题1:安装openssh-server失败 原因: 下列软件包有未满足的依赖关系: openssh-server : 依赖: openssh-client (= 1:5.9p1-5ubuntu1) ...
- Romantic(裸扩展欧几里德)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 关于MyEclipse启动时的插件启动(Maven4MyEclipse)
在myEclipse的应用中有许多插件在开发的时候都用不到,那么,这些插件在启动myEclipse的时候一起启动的越少越好了 Maven4Myeclipse update 每当启动myEclipse的 ...
- Iterator(迭代器)接口 --对象循环遍历
<?php class MyIterator implements Iterator { private $var = array(); public function __construct ...
- HBase 几点思考
1. http://blog.csdn.net/yueyedeai/article/details/14648067 2. http://blog.csdn.net/pirateleo/article ...
- C++堆和栈的比较(7个区别)
基础知识: 堆 栈是一种简单的数据结构,是一种只允许在其一端进行插入或删除的线性表.允许插入或删除操作的一端称为栈顶,另一端称为栈底,对堆栈的插入和删除操作被称 为入栈和出栈.有一组CPU指令可以实现 ...
- VC6中创建Qt工程的创建
文章来源:http://blog.sina.com.cn/s/blog_64d015c10100sf1o.html 本文主要介绍怎么创建可以在VC6中编译的QT工程.本文所采用环境为VC++6.0+Q ...