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 ...
随机推荐
- 学号160809212姓名田京诚C语言程序设计实验2选择结构程序设计
编写一个C程序,输入3个数,并按由大到小的顺序输出. 1 #include <stdio.h> void main(){ int a,b,c,t; printf("请输入三个整数 ...
- 让jar程序在linux上一直执行
当我们把java程序打成jar包后,放到linux上通过putty或其它终端执行的时候,如果按照:java -jar xxxx.jar执行,当我们退出putty或终端的时候,xxxx.jar这个程序也 ...
- PHP快速抓取快递信息
<?php header("Content-type:text/html;charset=utf-8"); /** * Express.class.php 快递查询类 * @ ...
- users
NAME users - print the user names of users currently logged in to the current host SYNOPSIS users [O ...
- 关于Xcode6 Segue 的疑问,没有解决!
xcode6 的segue 变化了,如图 关于前3个选项,始终没有太明白,我试验结果如下,简单地把几个viewController连接起来时,无论用show,还是showdetail,还是Presen ...
- Java中static静态关键字的使用
我们可以基于一个类创建多个对象,每个对象都拥有自己的成员,所有成员变量的值是根据对象而存在的,有些时候我们希望一个类的所有对象共享一个成员,这就用到了static静态关键字 被静态关键字修饰的成员属于 ...
- Python多线程(3)——Queue模块
Queue模块支持先进先出(FIFO)队列,支持多线程的访问,包括一个主要的类型(Queue)和两个异常类(exception classes). Python 2 中的Queue模块在Python ...
- BSD学习(BSD系统的历史和目标)
UNIX系统的历史 unix系统的发展历程大概经历以下几个阶段: 贝尔实验室(Bell Laboratories)阶段,该实验室发明了UNIX 加州大学伯克利分校(University of Cali ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- [Android Pro] PackageManager#getPackageSizeInfo (hide)
referce to : http://www.baidufe.com/item/8786bc2e95a042320bef.html 计算Android App所占用d的手机内存(RAM)大小.App ...