当前位置: 首页 > news >正文

【笔记】最长递增子序列 Longest increasing subsequence(LIS)

介绍和解法,参见wikipedia https://en.wikipedia.org/wiki/Longest_increasing_subsequence

笔记:

在按下标顺序遍历序列 X 的过程中,记录所有不同长度 L 的增长最慢的递增子序列,因为序列是递增的,只需要记录其最大元素,记为 M[L]

比如,对于序列 5,8,9,6,7,在访问 6 之前,M[1]=5,M[2]=8,M[3]=9。

访问 6 时,长度为2 的增长最慢的递增子序列为5,6,因此 M[2] 变为 6。

同理,访问 7 时, M[3] 变为 7。

遍历结束后,M 的长度即为 LIS 的长度,但并不知道这个 LIS 是什么。

 为了得到 LIS,将原来 M 保存的数据改为该数据在序列 X 中的 index;

并且,在遍历 X 的过程中,记录每个元素 X[i] 在候选的递增子序列中的前驱元素在序列 X 中的 index,记为P[i]

这样在遍历结束后,就可以从后往前输出 LIS:记 M 长度为 m,有:

LIS[m]   = X[    M[m]  ]
LIS[m-1] = X[  P[M[m]] ]
LIS[m-2] = X[P[P[M[m]]]]
......

 

wikipedia 原文引用如下:

It processes the sequence elements in order, maintaining the longest increasing subsequence found so far. Denote the sequence values as X[0], X[1], etc. Then, after processing X[i], the algorithm will have stored values in two arrays:

M[ j] — stores the index k of the smallest value X[ k] such that there is an increasing subsequence of length j ending at X[ k] on the range ki. Note that j(i+1), because j ≥ 1 represents the length of the increasing subsequence, and k ≥ 0 represents the index of its termination.
P[ k] — stores the index of the predecessor of X[ k] in the longest increasing subsequence ending at X[ k].

 

 P = array of length N
 M = array of length N + 1

 L = 0
 for i in range 0 to N-1:
   // Binary search for the largest positive j ≤ L
   // such that X[M[j]] < X[i]
   lo = 1
   hi = L
   while lo ≤ hi:
     mid = ceil((lo+hi)/2)
     if X[M[mid]] < X[i]:
       lo = mid+1
     else:
       hi = mid-1

   // After searching, lo is 1 greater than the
   // length of the longest prefix of X[i]
   newL = lo

   // The predecessor of X[i] is the last index of 
   // the subsequence of length newL-1
   P[i] = M[newL-1]
   M[newL] = i

   if newL > L:
     // If we found a subsequence longer than any we've
     // found yet, update L
     L = newL

 // Reconstruct the longest increasing subsequence
 S = array of length L
 k = M[L]
 for i in range L-1 to 0:
   S[i] = X[k]
   k = P[k]

 return S

 

 

相关问题:

最长先递增再递减子序列  牛客网 https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4?tpId=37&tqId=21247&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking

看了解法,思路如下:

遍历序列 X,得到以 X[i] 结尾的 LIS 的长度 Li[i] 和以 X[i] 开始的 LDS 的长度 Ld[i], 找到 Li[i] + Ld[i] 的最大值;

将 X 反向再求 LIS  即最长递减子序列 LDS;

在上述解法中加入 Li[i]:Li[i] = L[P[i]] + (newL > L) ? 1 : 0

 

最长公共子序列 Longest Common Subsequence(LCS)  可参见算法导论

TODO: Longest Common Subsequence for Multiple Sequences https://stackoverflow.com/questions/5752208/longest-common-subsequence-for-multiple-sequences

 

转载于:https://www.cnblogs.com/albumcover/p/9310174.html

相关文章:

  • c# 修改xml格式config文件
  • 【知识碎片】第三方登录弹窗效果
  • VMware虚拟机中为Linux 添加虚拟硬盘(VirtualBox方法类似)
  • Robot Framwork 问题小记
  • 给MySQL增加一个表示例
  • 复变函数:复函数的空间与Montel定理
  • sed使用命令记录
  • db2模式
  • 配置企业库5.0管理
  • SuperMicro(超微)IPMI安装操作系统KVM教程-超微3U8刀服务器
  • Python cookbook笔记——求N个最大最小元素及lambda表达式
  • restful 学习地址
  • Flutter 开发一个 GitHub 客户端 | 掘金技术征文
  • brk/sbrk的使用
  • 我们要和你完成一件大事
  • [Vue CLI 3] 配置解析之 css.extract
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • angular2开源库收集
  • CentOS从零开始部署Nodejs项目
  • Hibernate最全面试题
  • input的行数自动增减
  • Otto开发初探——微服务依赖管理新利器
  • Promise面试题,控制异步流程
  • 基于axios的vue插件,让http请求更简单
  • puppet连载22:define用法
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • # .NET Framework中使用命名管道进行进程间通信
  • #include
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (14)Hive调优——合并小文件
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (理论篇)httpmoudle和httphandler一览
  • (转) ns2/nam与nam实现相关的文件
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)linux 命令大全
  • .describe() python_Python-Win32com-Excel
  • .NET delegate 委托 、 Event 事件
  • .NET Framework 4.6.2改进了WPF和安全性
  • .NET MVC第三章、三种传值方式
  • .net 受管制代码
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .Net的C#语言取月份数值对应的MonthName值
  • @SentinelResource详解
  • [Head First设计模式]策略模式
  • [IE编程] 如何编程清除IE缓存
  • [MAUI]集成高德地图组件至.NET MAUI Blazor项目
  • [mvc] 简单的forms认证
  • [Mvc]在ASP.NET MVC中使用Repeater
  • [Oracle]4--查询操作
  • [pytorch入门] 6. 神经网络
  • [SDOI2017]数字表格
  • [Ubuntu 20.04] 使用Netplan配置网络静态IP
  • [Vue]路由传参 命名路由