LCS(打印全路径) POJ 2264 Advanced Fruits
题意:两个字符串结合起来,公共的字符只输出一次
分析:LCS,记录每个字符的路径
代码:
/*
LCS(记录路径)模板题:
用递归打印路径:)
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std; const int N = 1e2 + 10;
const int INF = 0x3f3f3f3f;
char s[N], t[N];
int dp[N][N];
int fa[N][N]; void print(int x, int y) {
if (!x && !y) return ;
if (fa[x][y] == 0) {
print (x-1, y-1); printf ("%c", s[x-1]);
}
else if (fa[x][y] == -1) {
print (x-1, y); printf ("%c", s[x-1]);
}
else {
print (x, y-1); printf ("%c", t[y-1]);
}
} void LCS(void) {
int lens = strlen (s), lent = strlen (t);
memset (dp, 0, sizeof (dp));
memset (fa, 0, sizeof (fa));
for (int i=0; i<=lens; ++i) fa[i][0] = -1;
for (int i=0; i<=lent; ++i) fa[0][i] = 1; for (int i=1; i<=lens; ++i) {
for (int j=1; j<=lent; ++j) {
if (s[i-1] == t[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;
}
}
} // printf ("%d\n", dp[lens][lent]);
print (lens, lent); puts ("");
} int main(void) {
while (scanf ("%s %s", &s, &t) == 2) {
LCS ();
} return 0;
}
LCS(打印全路径) POJ 2264 Advanced Fruits的更多相关文章
- poj 2264 Advanced Fruits(DP)
Advanced Fruits Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1944 Accepted: 967 ...
- HDU-1053:Advanced Fruits(LCS+路径保存)
链接:HDU-1053:Advanced Fruits 题意:将两个字符串合成一个串,不改变原串的相对顺序,可将相同字母合成一个,求合成后最短的字符串. 题解:LCS有三种状态转移方式,将每个点的状态 ...
- Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Advanced Fruits(好题,LCS的模拟)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- poj 2264(LCS)
Advanced Fruits Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2158 Accepted: 1066 ...
- 使用CCUserDefault 推断用户是否是第一次登陆系统及UserDefault全路径的获取
bool bfirst =CCUserDefault::sharedUserDefault()->getBoolForKey("first"); //假设不能获取该键值,创建 ...
- hdu 1503 Advanced Fruits(最长公共子序列)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- java实现遍历文件目录,根据文件最后的修改时间排序,并将文件全路径存入List集合
package com.ultra.aliyun.control.main; import java.io.File; import java.util.ArrayList; import java. ...
随机推荐
- [BZOJ1529][POI2005]ska Piggy banks
[BZOJ1529][POI2005]ska Piggy banks 试题描述 Byteazar 有 N 个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. Byteazar 已经把每个存钱罐的钥匙放 ...
- tomcat配置文件之Server.xml
Server.xml包含的元素有<Server>.<Service>.<Connector>.<Engine>.<Host>.<Con ...
- i686和x86_64的区别
找回TCL隐藏分区(转载) 用Wubi安装 Ubuntu 出现(Initranfs)问题的解决方案 i686和x86_64的区别 2009-04-11 08:19:31| 分类: 电脑问题 | 标 ...
- ubuntu安装到选择位置时闪退
转自:http://tieba.baidu.com/p/3020839207
- 【JavaScript】SVG vs Canvas vs WebGL
参考资料: http://blog.csdn.net/lufy_legend/article/details/38292125 http://zhidao.baidu.com/link?url=e4n ...
- 【Python】python代码如何调试?
Python 程序如何高效地调试? 现在我在debug python程序就只是简单在有可能错误的地方print出来看一下,不知道python有没像c++的一些IDE一样有单步调试这类的工具?或者说各位 ...
- Python DBUtils
1 简介 DBUtils是一套Python数据库连接池包,并允许对非线程安全的数据库接口进行线程安全包装.DBUtils来自Webware for Python. DBUtils提供两种外部接口: P ...
- x:Name标记特性与Name属性
本文转载自silvergingko的专栏 在Xaml中定义了一个元素后,如果后面要使用该元素,则必须为该元素定义一个元素名称,在随后的Xaml中,通过元素名称来使用该元素. 在Xaml中,元素的名称定 ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- hdu 1022 Train Problem I 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 又是一道栈的练习,这次也是没有用到STL中的栈来实现.用来保存操作过程的数组(process[] ...