1458 Common Subsequence
最简单的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的更多相关文章
- 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 ...
- 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
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46387 Accepted: 19 ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- Poj 1458 Common Subsequence(LCS)
一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
随机推荐
- 传值与传引用(C++)
reference(引用) 是C++对C的一个扩充 int a; int &b = a;//声明b是一个整形的引用变量 C语言,函数的参数传递有2种形式:传值方式调用和传引用方式调用 传值方式 ...
- Html Link 标签
Html Link 标签 Link 是 HTML Head 内部标签 <html> <head> <!-- link标签:rel="shortcut icon& ...
- 前后端分离不可缺少的神器 NGINX
样例讲解 1:安装工具包 wget.vim和gcc yum install -y wget yum install -y vim-enhanced yum install -y make cmake ...
- 【Mac AndroidStudio】download gradle fail问题
第一次运行application时,会发现一直在download gradle,而且进度一直卡着.这时,可以直接拷贝download的链接,粘贴到浏览器下载.下载完了.然后,可以用命令行在用户目录下o ...
- 超简单的localStorage实现记住密码的功能
HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前,这些都是由 coo ...
- 《温故而知新》JAVA基础六
多态(父子类之间) 对象的多种形态 引用多态 父类的引用可以指向本类对象 父类的引用可以指向子类的对象 方法的多态 创建本类对象时候,调用的方法是本类方法 创建子类对象时候,调用的方法为子类重写的方法 ...
- gis和threejs的学习资料
*********************************** 基础知识/名词 瓦片/矢量瓦片GeoJson - 绘制GeoJson看数据, geojson规范, 中文版 ********** ...
- Session 和 Cookie的区别
2019-03-26 18:16:47 一.区别概论 Session是在服务端保存的一个数据结构,用来跟踪用户的状态,这个数据可以保存在集群.数据库.文件中:Cookie是客户端保存用户信息的一种机制 ...
- The `android.dexOptions.incremental` property is deprecated and it has no effect on the build process.
编译报错:The android.dexOptions.incremental property is deprecated and it has no effect on the build pro ...
- 菜鸟脱壳之脱壳的基础知识(三)——寻找OEP
这节我们来讲讲如何寻找一个程序的OEP,即Original Entry Point.一些PE加壳程序在被加密的程序上面加了一个区段(有的壳也会合并区段),当外壳代码执行完毕以后,会跳到程序的本身的代码 ...