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 ...
随机推荐
- 追求代码质量: 用 AOP 进行防御性编程
原文出处: IBM中国 开发人员测试的主要缺点是:绝大部分测试都是在理想的场景中进行的.在这些情况下并不会出现缺陷 —— 能导致出现问题的往往是那些边界情况. 什么是边界情况呢?比方说,把 null ...
- ffmpeg 编码h264 profile如何设置为baseline的问题
http://blog.csdn.net/kisaa133/article/details/7792008 使用最新版ffmpeg-0.11 libx264-125,使用默认编码时,用Eyecard发 ...
- HTML预览 正则替换
1. [代码][PHP]代码 <?php if(!defined('BASEPATH')) exit('No direct script access allowed'); /** ...
- python pickle/cPickle模块
序列化(picking): 把变量从内存中变成可存储或传输的过程称为序列化,序列化之后,就可以把序列化的对象写入磁盘,或者传输给其他设备; 反序列化(unpickling):相应的,把变量的内容从序列 ...
- tcpdump 探测器分析
注:默认情况下,tcpdump临听它遇见的第一个网络接口,如果它选择了错误的接口,可以-i标志强行指定接口,如果DNS不能用,或者只是不希望tcpdump进行名字查找,请使用-n选项,这个选项(-n) ...
- multi_socket
threading_test.py #threading #为什么在命令行可以执行,F5不能执行 #线程处理能导致同步问题 from socketserver import TCPServer,Thr ...
- 深度学习之卷积神经网络(CNN)学习
1.卷积神经网络中卷积的核心意义是什么?每一组卷集核 权重是一个抽特征的滤波器, 从卷集核的角度抽取特征 2.卷积神经网络很好的特性参数共享机制每一个神经元固定一组a x b x c(图像的通道数) ...
- 在WinDBG中查看调用栈的命令
命令 ========== k k命令显示的是一定数量的栈帧, 其中帧的数量是由.kframes命令来控制的, 默认值是256. kp 5 显示调用栈中前5个函数以及他们的参数. kb 5 显示调用栈 ...
- python虚拟环境管理包virtualenvwrapper
1.打开cmd 2.安装virtualenvwrapper pip install virtualenvwrapper-win 3.配置虚拟环境的位置 新建系统变量默认在c盘 4.新建虚拟环境 mkv ...
- 23.java方法的深入
深入: public class MethodTest05{ public static void main(String[] args){ int i=m1(ture); System.out.pr ...