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 ...
随机推荐
- ffplay 一些好玩的filter
添加字幕:ffplay -vf drawtext="fontfile=arial.ttf: text='Test Text': x=100: y=300: \ fontsize=48: fo ...
- 在js里UTF-8与GB2312的互转
js的函数如下: function GB2312UTF8() { this.Dig2Dec = function(s) { var retV = 0; if (s.length == 4) { for ...
- Vue父子组件间的通信
父组件通过 props 向下传递数据给子组件,子组件通过 events 向上给父组件发送消息. 父组件: <div> <div style="background:#344 ...
- Android Jni(Java Native Interface)笔记
首先记录一个问题,关于如何用javah生成头文件. 为什么要生成头文件?在含有 static{ System.loadLibrary("hellojni"); } 这样代码的类下面 ...
- webstorm怎样查找历史记录
在webstorm中 文件界面右键,local History --> show History 上面能看到具体的日期和编写的代码. 如果想回到某一次的代码.把中间的代码按>>移入到 ...
- caffe参数详解
转载自:https://blog.csdn.net/qq_14845119/article/details/54929389 solver.prototxt net:训练预测的网络描述文件,trai ...
- C++之PIMPL模式
1 PIMPL解释 PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏. ...
- 为Xen虚拟机扩容根文件系统(LVM)
===== 为Xen虚拟机扩容根文件系统(LVM) ===== 1. 增加1个4G的映像文件 # dd if=/dev/zero of=data.img bs=4k seek=1024k count= ...
- Visual Studio Ultimate 2013 下载地址
VS2013_RTM_ULT_CHS.iso 文件大小:2.87G 百度网盘下载地址: http://pan.baidu.com/s/1bn4gavX 微软官网下载地址: http://downloa ...
- Android实例1:button点击响应
个人网站http://www.ravedonut.com/ Layout xml文件 <RelativeLayout android:layout_width="wrap_conten ...