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 ...
随机推荐
- Oracle 11g透明网关连接Sqlserver 2000
一.环境 公司网站系统使用的是IIS + Oracle 但公司某系统使用的是Sqlserver 2000, 但其数据需要做成报表放到网站上,为简化编程,使用Oracle做透明网关,定期从Sqlserv ...
- 帝国cms7.0 列表模板调用本栏目缩略图
[e:loop={"select classimg from phome_enewsclass where classid='$GLOBALS[navclassid]'",1,24 ...
- 输出1到最大的N位数
题目:输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. 分析:这是一道很有意思的题目.看起来很简单,其实里面却有不少的玄机. 应聘者在解决这个问 ...
- HDU 1847 Good Luck in CET-4 Everybody!
题解:巴什博弈,2^k+1=3N或2^k2=3N,所以3N为P-position,3N+r为N-position. #include <cstdio> int main(){ int n; ...
- 一道月薪3W的java面试题 (小明和小强都是张老师的学生,张老师的生日是某月某日,2人都不知道张老师的生日)
小明和小强都是张老师的学生,张老师的生日是M月N日,2人都知道张老师的生日 是下列10组中的一天,张老师把M值告诉了小明,把N值告诉了小强,张老师问他们知道他的生日是那一天吗? 3月4日 3月5日 3 ...
- Qt编写文件一键命名软件
之所以会写这篇博文,主要是由于近期从网上下载了一堆图片,但图片名称非常没有规律,处理起来非常不方便,由此想到是不是有一键命名的软件能够帮助我对全部图片命名,是图片名称有规律,这样在处理时方便操作. 有 ...
- JavaScript中cookie的路径(path)和域(domain)
cookie虽然是由一个网页所创建,但并不只是创建cookie的网页才能读 取该cookie.在默认情况下,与创建cookie的网页在同一目录或子目录下的所有网页都可以读取该cookie.但如果在这个 ...
- Html.text(转载)
2.Html.ValidationSummary:用来显示ModelState字典中所有验证错误的无序列表,使用布尔值类型参数(true)来告知辅助方法排除属性级别的错误,只显示ModelState中 ...
- JavaSE学习总结第03天_Java基础语法2
03.01 数据类型中补充的几个小问题 1:在定义Long或者Float类型变量的时候,要加L或者f. 整数默认是int类型,浮点数默认是double. byte,short在定义的时候, ...
- html5之histroy浅析
history是HTML5的新特性,我们可以使用它操作这个历史记录堆栈. (1)history提供了对浏览器历史纪录堆栈的读取,同时实现在访问记录中的前进和后退: history.length 历史记 ...