动态规划-LCS、LIS
LCS(Longest Common Subsequence)最长公共子序列。给定两个序列a和b,当另一序列c即是a的子序列又是b的子序列时,称c时a和b的公共子序列,最长公共子序列时所有子序列中长度最长的。 注意子序列是在原序列中删去若干元素后得到的序列,注意子序列≠子串,子串在原序列中是连续的。
Problem Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line. Sample Input abcfbc abfcab programming contest abcd mnp Sample Output 4 2 0
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
char a[maxn], b[maxn];
int dp[maxn][maxn];
int main() {
while (~scanf("%s%s", a + , b + )) {
memset(dp, , sizeof(dp));
int len1 = strlen(a + );
int len2 = strlen(b + );
for (int i = ; i <= len1; i++)
for (int j = ; j <= len2; j++)
if (a[i] == b[j])dp[i][j] = dp[i - ][j - ] + ;
else dp[i][j] = max(dp[i][j - ], dp[i - ][j]);
printf("%dn", dp[len1][len2]);
}
return ;
}
//滚动数组版后文有
high[] |
d[] |
len |
---|---|---|
1 5 6 2 3 4 |
1 |
1 |
1 5 6 2 3 4 |
1 5 |
2 |
1 5 6 2 3 4 |
1 5 6 |
3 |
1 5 6 2 3 4 |
1 2 6 |
3 |
1 5 6 2 3 4 |
1 2 3 |
3 |
1 5 6 2 3 4 |
1 2 3 4 |
4 |
结束后len=4,即LIS长度为4。
Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹. 怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统. Input 输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔) Output 对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统. Sample Input 8 389 207 155 300 299 170 158 65 Sample Output 2
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int n, high[maxn];
int LIS1() { //法一
int high2[maxn],dp[][maxn] = { };//滚动数组
memcpy(high2, high, sizeof(high));
sort(high2 + , high2 + n + );
for (int i = ; i <= n; i++) //LCS
for (int j = ; j <= n; j++)
if (high[i] == high2[j])
dp[i & ][j] = dp[i & ? : ][j - ] + ;
else
dp[i & ][j] = max(dp[i & ? : ][j], dp[i & ][j - ]);
return dp[n&][n];
}
int LIS2() { //法二
int dp[maxn], ans = ;
for (int i = ; i <= n; i++) {
dp[i] = ;
for (int j = ; j < i; j++)
if (high[j] < high[i])
dp[i] = max(dp[j] + , dp[i]);
ans = max(ans, dp[i]);
}
return ans;
}
int LIS3() { //法三
int d[maxn], len = ;
d[] = high[];
for (int i = ; i <= n; i++)
if (high[i] > d[len])
d[++len] = high[i];
else { //用lower_bound()查询第一个>high[i]的地址
int j = lower_bound(d + , d + len + , high[i]) - d;
d[j] = high[i];
}
return len;
}
int main() {
while (cin>>n) {
for (int i = ; i <= n; i++)
cin >> high[i];
//cout << LIS1() << "n";
//cout << LIS2() << "n";
cout << LIS3() << "n";
}
return ;
}
原创不易,请勿转载(
本不富裕的访问量雪上加霜) 博主首页:https://blog.csdn.net/qq_45034708
- 关于权限设置的一个小把戏(r2第27天)
- R语言实现混合模型
- 数据迁移判断非空约束(r2笔记45天)
- 【专业技术】linux下socket编程
- oracle并行的小细节(r2笔记44天)
- python实现朴素贝叶斯模型:文本分类+垃圾邮件分类
- 使用闪回查询备份数据(r2笔记43天)
- 生产环境sql语句调优实战第四篇(r2笔记41天)
- 生产环境sql语句调优实战第五篇(r2笔记41天)
- python实现逻辑logistic回归:预测病马的死亡率
- 开发 | 图片数据集太少?看我七十二变,Keras Image Data Augmentation 各参数详解
- linux过滤空文件的命令总结(r2笔记40天)
- shell脚本自动化采集性能sql(r2笔记39天)
- R语言与点估计学习笔记(EM算法与Bootstrap法)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [Centos7]自定义开机自启脚本
- [Centos7]安装scapy模块
- [Centos7]安装rrdtool模块
- [Centos7]XlsxWriter模块安装
- [Centos7]安装pycurl
- [Centos7]python-nmap端口扫描
- [docker]Nginx安装部署
- Lua语言-入门语法
- Redis 三种启动方式
- OpenWRT结合tinc组自己的SDLAN
- 通过Cloudflare API更新DNS记录
- Vedastr:基于PyTorch的场景文本识别工具箱
- vue-element-admin项目关闭eslint校验
- 搞了半天,终于弄懂了TCP Socket数据的接收和发送,太难~
- 每天一个小技巧:CSS clip-path 的妙用 Clip Path分类Clippy