POJ 1458 Common Subsequence DP
http://poj.org/problem?id=1458
用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS。
1、如果str[i] == sub[j],那么LCS长度就可以+1,是从dp[i - 1][j - 1] + 1,因为是同时捂住这两个相同的字符,看看前面的有多少匹配,+1后就是最大长度。
2、如果不同,那怎么办? 长度是肯定不能增加的了。
可以考虑下删除str[i] 就是dp[i - 1][j]是多少,因为可能i - 1匹配了第j个。也可能删除sub[j],就是dp[i][j - 1],因为可能str[i] == sub[j - 1]。同时考虑这两种情况的话,就是取max了。
因为只和上一维有关,所以可以用滚动数组来完成。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> const int maxn = 1e4 + ;
char str[maxn];
char sub[maxn];
int dp[][maxn];
void work() {
int now = ;
int lenstr = strlen(str + );
int lensub = strlen(sub + );
memset(dp, , sizeof dp);
for (int i = ; i <= lenstr; ++i) {
for (int j = ; j <= lensub; ++j) {
if (str[i] == sub[j]) {
dp[now][j] = dp[!now][j - ] + ;
} else {
dp[now][j] = max(dp[now][j - ], dp[!now][j]);
}
}
now = !now;
}
cout << dp[!now][lensub] << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
while (cin >> str + >> sub + ) work();
return ;
}
POJ 1458 Common Subsequence DP的更多相关文章
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- poj 1458 Common Subsequence(dp)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46630 Accepted: 19 ...
- OpenJudge/Poj 1458 Common Subsequence
1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- POJ 1458 Common Subsequence (动态规划)
题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
随机推荐
- Java8初体验(2):Stream语法详解
原文出处: 一冰_天锦 上篇文章Java8初体验(1):lambda表达式语法比较详细的介绍了lambda表达式的方方面面,细心的读者会发现那篇文章的例子中有很多Stream的例子.这些Stream的 ...
- codeforces 465C.No to Palindromes! 解题报告
题目链接:http://codeforces.com/problemset/problem/464/A 题目意思:给出一个长度为 n 且是 tolerable 的字符串s,需要求出字典序最小的长度也为 ...
- struts2 session登录
session:记录于服务器端的信息,当客户端传来信息时候,判断是不是指定的信息. 常见应用:判断用户是否登录. struts具体的实现不写了,写主要的. 在action的方法中加入: ActionC ...
- PGTM通用性能测试模型
PTGM通用性能测试模型 一. 测试前期准备阶段 目标: 1. 保证系统稳定性: 2. 建立合适的测试团队. 活动: 1. 系统基础功能验证 类似于BVT测试,确保被测系统已具备进行性 ...
- HDU1852 Beijing 2008(快速幂+特殊公式)
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...
- cogs1070玻璃球游戏
1070. [焦作一中2012] 玻璃球游戏 ★ 输入文件:marbles.in 输出文件:marbles.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 小x ...
- Microsoft Speech SDK开发包 使用
下载开发包.我们首先从微软的官网上面下载开发包,下载地址如下: http://www.microsoft.com/en-us/download/details.aspx?id=10121我们主要下载三 ...
- spark运行模式之二:Spark的Standalone模式安装部署
Spark运行模式 Spark 有很多种模式,最简单就是单机本地模式,还有单机伪分布式模式,复杂的则运行在集群中,目前能很好的运行在 Yarn和 Mesos 中,当然 Spark 还有自带的 Stan ...
- Word2013 在一个页面双列显示
1. 效果图 2. 实现方法 (1) 进入页面布局 (2) 选中要整理的字,选中Columns,然后选择Two
- 如何使用最简单的方法将一个已经存在的工程中使用 cocaPodfile
在网上搜索的使用 cocaPods 安装一些优秀的框架,搜索的博客大多步骤都是非常的麻烦,这里的方法非常的简单,本篇仅仅作为以后备用. 第一步:首先找到我们的工程,在终端中输入 cd 拖入已经存在 ...