回顾经典问题算法:LIS, LCS-(DP类别)
LIS,最长递增子序列
说明见:http://blog.csdn.net/sdjzping/article/details/8759870
#include <iostream>
#include <cstdlib> using namespace std; int LIS(int* arr, int len) {
if (arr == NULL || len < ) return ;
int LIS[len] = {};
int mlen = ;
for (int i=; i<len; i++) {
LIS[i] = ;
for (int j = ; j < i; j++) {
if (arr[j] < arr[i] && LIS[i] < LIS[j] + ) {
LIS[i] = LIS[j] + ;
}
}
if (LIS[i] > mlen) {
mlen = LIS[i];
}
}
return mlen;
} int main() {
int arr[] = {, -, , -, , -, , -};
int len = sizeof(arr) / sizeof(arr[]);
cout<<LIS(arr, len)<<endl;
system("pause");
return ;
}
再给出nlogn的解法,其中len2val数组就是记录当前LIS长度时结尾的数字(可能情况下的最小值,如1,3和1,2同样可以构成长度为2的LIS但是取len2val[2]=2),关于这个数组的详细说明,可以参考第一个给出的连接,相对于第一个算法的改进就是该数组是有序的,可以使用二分搜索,即在数组中找到第一个比待处理值V大或相等的index设为I(就是序列长度),那么这个V必然可以插入到以len2val[I-1]结尾的长度为I-1的序列后部,形成新序列的长度为I,因而更新数组len2val[I] = V(更新是因为当前值肯定是大于等于V的,因为一开始就是根据这个条件进行搜索的)。这个查找的过程可以用STL中的lower_bound来做,这里自己按照源码写了一个直接返回索引的版本,STL中返回的是迭代器(数组迭代器就是元素指针,比较时还需减掉数组起始位置,才能得到索引)。
int idx_lower_bound(int a[], int begin, int end, int target) {
int count = end - begin;
int first = begin;
while (count > ) {
int step = count/;
int idx = first + step;
if (a[idx] < target) {
first = ++idx;
count = count - step - ;
} else {
count = step;
}
}
return first;
}
int LIS_NLOGN(int arr[], int len) {
if (arr == NULL || len < ) {
return ;
}
int* len2idx = new int[len+];
len2idx[] = arr[];
int maxlen = ;
for (int i=; i<len; i++) {
char ch = arr[i];
int upper = idx_lower_bound(len2idx, , maxlen + , ch);
len2idx[upper] = ch;
if (upper > maxlen) {
maxlen = upper;
}
}
return maxlen;
}
LCS,最长公共子序列
#include <iostream>
#include <cstdlib>
#include <cstring> using namespace std; int max(int a, int b) {
return a > b ? a : b;
} int LCS(const char* s1, int len1, const char* s2, int len2) {
if (s1 == NULL || s2 == NULL || len1 < || len2 < ) return ; int dp[len1 + ][len2 + ]; for (int i=; i<=len1; i++) {
for (int j=; j<=len2; j++) {
dp[i][j] = ;
}
} for (int i=; i<= len1; i++) {
for (int j=; j<=len2; j++) {
if (s1[i-] == s2[j-]) {
dp[i][j] = dp[i-][j-] + ;
} else {
dp[i][j] = max(dp[i-][j], dp[i][j-]);
}
}
}
for (int i=; i<=len1; i++) {
for (int j=; j<=len2; j++) {
cout<<" "<<dp[i][j];
}
cout<<endl;
}
return dp[len1][len2];
}; int main() {
const char* s1 = "helloyyc";
const char* s2 = "xellxddc"; cout<<LCS(s1, strlen(s1), s2, strlen(s2))<<endl; system("pause");
return ;
}
回顾经典问题算法:LIS, LCS-(DP类别)的更多相关文章
- LIS LCS n^2和nlogn解法 以及LCIS
首先介绍一下LIS和LCS的DP解法O(N^2) LCS:两个有序序列a和b,求他们公共子序列的最大长度 我们定义一个数组DP[i][j],表示的是a的前i项和b的前j项的最大公共子序列的长度,那么由 ...
- 【十大经典数据挖掘算法】AdaBoost
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 集成学习 集成学习(ensem ...
- 【十大经典数据挖掘算法】SVM
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART SVM(Support Vector ...
- 【十大经典数据挖掘算法】Naïve Bayes
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 朴素贝叶斯(Naïve Bayes) ...
- 【十大经典数据挖掘算法】C4.5
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 决策树模型与学习 决策树(de ...
- 【十大经典数据挖掘算法】k-means
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 引言 k-means与kNN虽 ...
- 【十大经典数据挖掘算法】kNN
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 引言 顶级数据挖掘会议ICDM ...
- 【十大经典数据挖掘算法】CART
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 前言 分类与回归树(Class ...
- 【十大经典数据挖掘算法】k
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 引言 k-means与kNN虽 ...
随机推荐
- mysql 查看索引
查看索引 mysql> show index from tblname; mysql> show keys from tblname; · Table 表的名称. · Non_unique ...
- 第四天,同步和异常数据存储到mysql,item loader方法
github对应代码:伯乐在线文章爬取 一. 普通插入方法 1. 连接到我的阿里云,用户名是test1,然后在navicat中新建数据库
- 火焰图定位dbproxy问题
https://blog.csdn.net/oujiangping/article/details/78580450 https://blog.csdn.net/gatieme/article/det ...
- ObjectMapper 动态用法
class DymicObject { private Object o; public DymicObject(Object o) { this.o = o; } p ...
- L03-Linux RHEL6.5系统中配置本地yum源
1.将iso镜像文件上传到linux系统.注意要将文件放在合适的目录下,因为后面机器重启时还要自动挂载,所以此次挂载成功之后该文件也不要删除. 2.将iso光盘挂载到/mnt/iso目录下. (1)先 ...
- windows jenkins 卸载
如果下载的是war包,先在任务管理器上停止jenkins的服务,再删除jenkins整个文件
- 搭建django环境
一.安装django(两种方式) 1.pip install django 2.python setup.py install(下载gjango包:https://www.djangoproject. ...
- java.lang.Exception: The server rejected the connection: None of the protocols were accepted
solution for this is from comment for https://issues.jenkins-ci.org/browse/JENKINS-29616. The follow ...
- Django中-事务操作
如何在Django中进行事务操作呢? 近期,公司里要使用Django开发一套金融相关的系统. 涉及钱了.....安全安全安全 如果钱转到一半,系统崩了,咋办? 如果钱汇到一半,系统崩了,咋办? 如果东 ...
- $scope作用域与依赖注入
一.$scope与$rootscope作用域 $scope下的数据作为该控制器下的数据moduel,只有在该控制器下才能够访问:而$rootScope则可以可以再任何有效的地方访问到,这个有效的地方指 ...