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 ...
随机推荐
- cocos基础教程(10)纹理缓存技术
Cocos2d通过调用CCTextureCache或者CCSpriteFrameCache来缓存精灵的纹理. 当这个精灵调用CCTextureCache 或 CCSpriteFrameCache的方法 ...
- PCL初步使用
转载:http://blog.csdn.net/vbskj/article/details/7819828 本次试验的目的是利用PCL库来重建地形点云数据,并进行显示.总体流程是1)把DEM数据导入P ...
- JS 自定义正则表达式
1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...
- Android判断网络是否连接
<!-- 配置文件判断网络是否连接 --> <uses-permission android:name="android.permission.ACCESS_NETWORK ...
- PHPNG (next generation)
PHPNG (next generation) This page gives short information about development state of a new PHP branc ...
- Easy Multiple Copy to Clipboard by ZeroClipboard
要实现在多个复制按钮复制的功能(具体代码在附件中,路径修改一下就行了): <%@ page language="java" import="java.util.*& ...
- macosx zsh下安装rvm和ruby
1)curl -L get.rvm.io | bash -s stable 2)把下面一行加到~/.zshrc中: [[ -s "$HOME/.rvm/scripts/rvm" ] ...
- ZOJ 2315
---恢复内容开始--- http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 这个题目比较难以看懂,我也是看网上的题目意思才 ...
- Objective-C ,C++,java中常用编码格式对比
这个题目可能不太对!主要总结一下这3种语言的不同格式 1.创建一个A类,继承B类,实现C接口(协议) 先看oc的代码 @interface A : B <C> { int a; } @pr ...
- 4.python函数基础
一.函数 1.函数简介 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但 ...