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 ...
随机推荐
- 多层神经网络与C++实现
BP理论部分参考:http://blog.csdn.net/itplus/article/details/11022243 参考http://www.cnblogs.com/ronny/p/ann_0 ...
- 深度学习入门教程UFLDL学习实验笔记一:稀疏自编码器
UFLDL即(unsupervised feature learning & deep learning).这是斯坦福网站上的一篇经典教程.顾名思义,你将在这篇这篇文章中学习到无监督特征学习和 ...
- poj1125最短路
Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30408 Accepted: ...
- 腾讯新浪通过IP地址获取当前地理位置(省份)的接口
腾讯新浪通过IP地址获取当前地理位置(省份)的接口 腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array(" ...
- 使用Discuz关键词服务器实现PHP中文分词
不同于使用自己的服务器进行分词,Discuz!在线中文分词服务是基于API返回分词结果的.在项目中,我们只需要一个函数即可方便地进行分词.关键词提取.以下是根据Discuz!在线分词服务API写的函数 ...
- 怎样取出cobbler kopts中设置的参数?
Is there a way to find out with what parameters did the kernel boot? For example if I specify noexec ...
- 【Python】python代码如何调试?
Python 程序如何高效地调试? 现在我在debug python程序就只是简单在有可能错误的地方print出来看一下,不知道python有没像c++的一些IDE一样有单步调试这类的工具?或者说各位 ...
- 使用shadow dom封装web组件
什么是shadow dom? 首先我们先来看看它长什么样子.在HTML5中,我们只用写如下简单的两行代码,就可以通过 <video> 标签来创建一个浏览器自带的视频播放器控件. <v ...
- python 异常类型
1.NameError:尝试访问一个未申明的变量>>> vNameError: name 'v' is not defined 2.ZeroDivisionError:除数为0&g ...
- C++基础知识面试精选100题系列(1-10题)[C++ basics]
[原文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-1-10.html [题目 ...