最简单的LIS;

设字符串为 a = acc  b = cc

则dp数组为

0 0

1 1

1 2

b[0] = a[1], b[1] = a[1] 防止这里算两个

要清楚的是 怎么不重复计算 也就是dp[1]的计算

首先dp[1][0] = 1;

再处理 dp[1][1], 因为 b[1] = a[1], 它是可以加一的 加哪一个呢? 要是直接继承左边的 选择dp[1][0] + 1 就会发生重复计算 因为b[0] = a[1], dp[1][0]已经是加上了一个共同字符的值

解决方法是 选择 dp[0][0]  + 1 也就是继承上一轮的dp值, 因为上一轮只搞了a[0], 没有倒腾a[1], 即使 b[0] = a[1]也没有算进去, 所以避免了重复, 以此类推

写成滚动数组

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <sstream>
#include <algorithm>
const int si = 1e4;
using namespace std;
int dp[][si]; int main() {
string sss, a, b;
while (getline(cin, sss)) {
stringstream ss(sss);
ss >> a;
ss >> b;
fill(dp[], dp[] + * si, );
int sa = a.size(), sb = b.size();
int e = ;
for (int i = ; i <= sa; i++) {
for (int j = ; j <= sb; j++) {
dp[e][j] = max(dp[ - e][j], dp[e][j - ]);
if (b.at(j - ) == a.at(i - )) {
dp[e][j] = max(dp[ - e][j - ] + , dp[e][j]);
//dp[1 - e][j - 1]是j-1在上一层没有匹配第i-1个的 所以不会重复计算
}
}
e = - e;
}
cout << dp[ - e][sb] << endl;
}
return ;
}

1458 Common Subsequence的更多相关文章

  1. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  2. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  3. OpenJudge/Poj 1458 Common Subsequence

    1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...

  4. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  5. POJ 1458 Common Subsequence (动态规划)

    题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...

  6. poj 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46387   Accepted: 19 ...

  7. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  8. (线性dp,LCS) POJ 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65333   Accepted: 27 ...

  9. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  10. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

随机推荐

  1. 运行maven install命令时出现错误(BUILD FAILURE)

    运行run as—>maven install时出现以下错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-com ...

  2. Python paramiko 修改源码实现用户命令抓取

    paramiko 源码修改 paramiko主要用来实现ssh客户端.服务端链接,上一节我们说到了堡垒机,堡垒机内有一个需求是“用户行为审计”,在这里我们就可以通过修改paramiko内文件的源码来实 ...

  3. Linux 管理服务启动工具

    工具:ntsysv(图形,可以关闭开启服务) 安装包:ntsysv-1.3.30.2-2.el5 工具:chkconfig –list(文字,开启关闭服务) 自定义加服务:执行脚本放入:/etc/in ...

  4. mac 遇到的奇怪问题?

    1: 卸载 xcode,发现git报错了. mac git xcrun error active developer path 错误 解决办法:sudo xcode-select -switch / ...

  5. bitbucket迁移

    bitbucket 迁移 1.停止向旧仓库地址提交代码 [dev]2.导入代码至新仓库地址 [op]3.修改本地仓库地址 第一种方式:git remote set-url origin [url] ; ...

  6. 【做题】SDOI2017硬币游戏——方程&概念处理

    原文链接 https://www.cnblogs.com/cly-none/p/9825339.html 题意:给出\(n\)个长度为\(m\)的互不相同的01串.有另一个串,初始为空.不断进行如下操 ...

  7. Postman接口调试神器

    Postman起初源自googleBrowser的一款插件,现已经有单独软件,当然之前的插件也存在  它主要是用于接口的的调试 接口请求的流程 一.GET请求 GET请求:点击Params,输入参数及 ...

  8. Learning-Python【22】:面向对象初识

    一.面向过程:是一种编程思想,核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么然后干什么,基于该编程思想写程序就好比在设计一条流水线,是一种机械式的思维方式 优点:把复杂问题流程化,进而简 ...

  9. mui框架下监听返回按钮

    用于监听mui框架下的Android手机的返回按键(物理键) mui.back = function() { if(b == true) {//一个标识符,在某个状态下不允许双击返回关闭程序 aler ...

  10. Cordova结合Vue学习Camera

    简单聊两句 学习Vue+Cordova打包编译App,首先你要安装Cordova与vue,在这里本人就不说明了,自行看文档与搜索相关资料. Cordova中文官网地址 Vue中文官网地址 第一步:首先 ...