最简单的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. P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers

    P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...

  2. JS设计模式(9)享元模式

    什么是享元模式? 定义:享元模式是一种优化程序性能的模式,本质为减少对象创建的个数. 主要解决:在有大量对象时,有可能会造成内存溢出,我们把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存 ...

  3. 【java】J2EE、J2SE和J2ME的区别

    本文向大家简单介绍一下J2EE.J2SE.J2ME概念及区别,J2EE,J2SE,J2ME是java针对不同的的使用来提供不同的服务,也就是提供不同类型的类库. Java2平台包括:标准版(J2SE) ...

  4. Go 使用 JSON

    Encode 将一个对象编码成 JSON 数据,接受一个 interface{} 对象,返回 []byte 和 err func Marshal(v interface{}) {[]byte,err} ...

  5. [从零开始搭网站八]CentOS使用yum安装Redis的方法

    1.由于centOS官方yum源里面没有Redis,这里我们需要安装一个第三方的yum源,这里用了fedora的epel仓库 yum install epel-release 安装过程中会有让你确认的 ...

  6. public,private,protected,以及default时的区别

    作用域    当前类   同一package     子孙类   其他package public       √                 √                    √    ...

  7. python tar 打包

    import os import tarfile def make_targz_one_by_one(output_filename, source_dir): tar = tarfile.open( ...

  8. 51Nod - 1384

    全排列函数解法 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...

  9. ActiveRecord Nested Atrributes 关联记录,对嵌套属性进行CURD

    设置了Nested attributes后,你可以通过父记录来更新/新建/删除关联记录. 使用: #accepts_nested_attributes_for class method. 例如: cl ...

  10. hdoj4685

    数据: /*999993 43 1 2 42 2 32 3 4*/ #include <iostream> #include <cstdio> #include <cma ...