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 ...
随机推荐
- zabbix部署相关
一.centos7 安装zabbix 二.zabbix 乱码问题 三.zabbix自动发现自动注册 四.zabbix3.4实现sendEmail邮件报警
- Dart - Isolate 并发
在Dart中实现并发可以用Isolate,它是类似于线程(thread)但不共享内存的独立运行的worker,是一个独立的Dart程序执行环境.其实默认环境就是一个main isolate. 在Dar ...
- Shell 实践、常用脚本
(1)计算1-100的和. #!/bin/bash n= ` do n=$[$i+$n] done echo $n (2)输一个数字,然后计算出1到数字的和,要求如果输入数字小于1,则重新输入,知道输 ...
- activiti5/6 系列之--Activiti与BPMN2.0规范相关节点对应关系
根据BPMN2.0规范的分类划分为以下部分: 1.启动与结束事件(event) 2.顺序流(Sequence Flow) 3.任务(Task) 4.网关(Gateway) 5.子流程(Subproce ...
- 利用JS打印质数
我爱撸码,撸码使我感到快乐!大家好,我是Counter,今天非常愉快,没有前几天的相对比较复杂的逻辑思维在里面,今天来写写,利用JS打印质数,基本上很多面试,会很经常的考到.那废话不多说,直接上代码: ...
- linux基础之grep
grep: Global search REgular expression and Print out the line 作用: 文本搜索工具,根据用户指定的模式对目标文本逐行进行匹配检查,打印匹配 ...
- 【Entity Framework】Revert the database to specified migration.
本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 [Entity Framework] Revert the database to specified migration. [ ...
- sublime汉化
1.打开sublime使用快捷键 shift+ctrl+p调出package control; 2.键入Package Control:install package 会弹出一个输入框,然后再搜索lo ...
- vue--vConsole
平时在web应用开发过程中,我们可以console.log去输出一些信息,但是在移动端,也就是在手机上,console.log的信息我们是看不到的. 这种情况下,可以选择使用alert弹出一些信息,但 ...
- 北京动点飞扬软件招募【Android全职工程师】
要求: 1 至少半年2年以上android开发经验,能力第一,学历不限 2 至少5个以上正规app开发经验 3 项目周期12个月左右,要求一周内到岗 4 有意向者简历请发邮箱372900288@qq. ...