LCS(打印路径) POJ 2250 Compromise
题意:求单词的最长公共子序列,并要求打印路径
分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] = max (dp[i-1][j], dp[i][j-1])
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std; const int N = 1e2 + 10;
const int M = 50;
const int INF = 0x3f3f3f3f;
char s1[M][N], s2[M][N];
int dp[N][N];
int fa[N][N];
bool fir; void print(int x, int y) {
if (!x && !y) return ; if (fa[x][y] == 0) {
print (x-1, y-1);
if (fir) {
printf ("%s", s1[x-1]); fir = false;
}
else printf (" %s", s1[x-1]);
}
else if (fa[x][y] == -1) print (x-1, y);
else print (x, y-1);
} void LCS(int len1, int len2) {
memset (dp, 0, sizeof (dp));
memset (fa, 0, sizeof (fa));
for (int i=0; i<=len1; ++i) fa[i][0] = -1;
for (int i=0; i<=len2; ++i) fa[0][i] = 1; for (int i=1; i<=len1; ++i) {
for (int j=1; j<=len2; ++j) {
if (!strcmp (s1[i-1], s2[j-1])) {
dp[i][j] = dp[i-1][j-1] + 1;
fa[i][j] = 0;
}
else if (dp[i-1][j] >= dp[i][j-1]) {
dp[i][j] = dp[i-1][j];
fa[i][j] = -1;
}
else {
dp[i][j] = dp[i][j-1];
fa[i][j] = 1;
}
}
} fir = true;
print (len1, len2); puts ("");
} int main(void) {
while (scanf ("%s", s1[0]) == 1) {
int c1 = 1, c2 = 1;
while (strcmp (s1[c1-1], "#")) scanf ("%s", &s1[c1++]);
c1--; scanf ("%s", s2[0]);
while (strcmp (s2[c2-1], "#")) scanf ("%s", &s2[c2++]);
LCS (c1, c2);
} return 0;
}
LCS(打印路径) POJ 2250 Compromise的更多相关文章
- POJ 2250 Compromise(LCS)
POJ 2250 Compromise(LCS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#proble ...
- POJ 2250 Compromise【LCS】+输出路径
题目链接:https://vjudge.net/problem/POJ-2250 题目大意:给出n组case,每组case由两部分组成,分别包含若干个单词,都以“#”当结束标志,要求输出最长子序列. ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- 最长公共子序列Lcs(打印路径)
给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这 ...
- POJ - 2250 Compromise (LCS打印序列)
题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...
- POJ2250 - Compromise(LCS+打印路径)
题目大意 给定两段文本,问公共单词有多少个 题解 裸LCS... 代码: #include<iostream> #include<string> using namespace ...
- poj 2250 Compromise(区间dp)
题目链接:http://poj.org/problem?id=2250 思路分析:最长公共子序列问题的变形,只是把字符变成了字符串,按照最长公共子序列的思路即可以求解. 代码如下: #include ...
- POJ 2250 Compromise (UVA 531)
LCS问题.基金会DP. 我很伤心WA非常多.就在LCS问题,需要记录什么路. 反正自己的纪录path错误,最后,就容易上当. 没有优化,二维阵列,递归打印,cin.eof() 来识别 end of ...
- 区间dp模型之括号匹配打印路径 poj(1141)
题目链接:Brackets Sequence 题目描写叙述:给出一串由'(')'' [ ' ' ] '组成的串,让你输出加入最少括号之后使得括号匹配的串. 分析:是区间dp的经典模型括号匹配.解说:h ...
随机推荐
- LVS负载均衡集群服务搭建详解(一)
LVS概述 1.LVS:Linux Virtual Server 四层交换(路由):根据请求报文的目标IP和目标PORT将其转发至后端主机集群中的某台服务器(根据调度算法): 不能够实现应用层的负载均 ...
- UIView 注意问题
1. UIView.userInteractionEnabled UIView.userInteractionEnabled默认值是YES http://blog.csdn.net/studyreco ...
- svn报错 400 Bad Request
MyEclipse中的svn,commit经常报错 Error: Commit failed (details follow): Error: At least one property chang ...
- 做网站用UTF-8还是GB2312 & 各国语言对应字符集
经常我们打开外国网站的时候出现乱码,又或者打开很多非英语的外国网站的时候,显示的都是口口口口口的字符, WordPress程序是用的UTF-8,很多cms用的是GB2312. ● 为什么有这么多编码? ...
- codeigniter 视图
2014年7月7日 15:23:05 ci的视图功能很棒, 比如一个网页有四个部分组成,对应4个文件:header.php, sider.php, maincontent.php, footer .p ...
- 【动态规划】盖房子(house)--未提交--已提交
问题 D: 盖房子(house) 时间限制: 1 Sec 内存限制: 64 MB提交: 27 解决: 16[提交][状态][讨论版] 题目描述 FJ最近得到了面积为n*m的一大块土地,他想在这块土 ...
- Linux下RPM、tar.gz、DEB格式软件包的区别
初接解Linux的朋友一定对软件的安装特别头疼,同样都是for Linux,但RPM.tar.gz.DEB包还是有很大区别的,这种区别很有可能使你的安装过程进行不下去.那我们应该下载什么格式的包呢 ...
- HDU 2147 kiki's game(博弈)
kiki's game Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit S ...
- Java性能优化权威指南-读书笔记(四)-JVM性能调优-延迟
延迟指服务器处理一个请求所花费的时间,单位一般是ms.s. 本文主要讲降低延迟可以做的服务器端JVM优化. JVM延迟优化 新生代 新生代大小决定了应用平均延迟 如果平均Minor GC持续时间大于应 ...
- eclipse 切换svn账号
1. 查看你的Eclipse中使用的是什么SVN Interface,位置在 windows > preference > Team > SVN 2. 如果是用的J ...