当前位置:网站首页>Longest non descent subsequence (LIS) (dynamic programming)
Longest non descent subsequence (LIS) (dynamic programming)
2022-07-07 05:08:00 【Madness makes freedom】
- State Design :dp[i] Said to str[i] The length of the longest subsequence at the end does not decrease
- State transition equation :dp[i]=max(dp[j]+1,1) (j<i&&str[j]<=str[i]);
- Of course , Later, we can also output the content of the longest non descending subsequence , Just enumerate from the back dp Decrement the value of , as long as dp The value of is equal to temp( The length of the longest subsequence decreases ) equal , Just put str[i] Value added to lis in , until temp==0 until .
- Output the content of the first longest non descending subsequence : Just start enumerating from the front .
/**
2) State Design :dp[i] Said to str[i] The length of the longest subsequence at the end does not decrease
State transition equation :dp[i]=max(dp[j]+1,1) (j<i&&str[j]<=str[i]);
Of course , Later, we can also output the content of the longest non descending subsequence , Just from the back
Start enumeration dp Decrement the value of , as long as dp The value of is equal to temp( The length of the longest subsequence decreases ) equal ,
Just put str[i] Value added to lis in , until temp==0 until .
Output the content of the first longest non descending subsequence : Just start enumerating from the front .
*/
/**
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
cout << "input a string:\n";
cin >> str;
int len=str.size();
int dp[len],ans=0; //dp[i] Said to str[i] The length of the longest subsequence at the end does not decrease
dp[0]=1; // State transition equation :dp[i]=max(dp[j]+1,1) (j<i&&str[j]<=str[i])
for(int i=1;i<len;++i)
{
dp[i]=1;
for(int j=0;j<i;++j)
{
if(str[j]<=str[i]&&dp[j]+1>dp[i])
dp[i]=dp[j]+1;
}
ans=max(ans,dp[i]);
}
string lis; //lis Output the longest non descending subsequence
int temp=ans;
for(int i=len-1;i>=0;--i)
{
if(dp[i]==temp)
{
lis=str[i]+lis; temp Look for the result from the back , When the longest sequence does not fall
--temp; When there is more than one result , The resulting sequence is the last group
}
}
cout << ans << endl;
for(auto a:str)
cout << a << ' ';
cout << endl;
for(auto a:dp)
cout << a << ' ';
cout << endl;
cout << lis << endl;
string first;
temp=1;
for(int i=0;i<len;++i)
{
if(dp[i]==temp)
{
first+=str[i];
++temp;
}
}
cout << "first substr:\n";
cout << first << endl;
return 0;
}
*/3) In order not to lower the content of the subsequence for subsequent output , You can also open one result Array ,result[i]
Express str[i] The predecessor node of , The first node of a sequence is set to -1, Distinguish from other nodes ,
In fact, it is used with the first program dp The output content is the same .
/**
3) In order not to lower the content of the subsequence for subsequent output , You can also open one result Array ,result[i]
Express str[i] The predecessor node of , The first node of a sequence is set to -1, Distinguish from other nodes ,
In fact, it is used with the first program dp The output content is the same
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
cout << "input a string:\n";
cin >> str;
int len=str.size();
int dp[len]; //dp[i] Said to str[i] The length of the longest subsequence at the end does not decrease
dp[0]=1; // State transition equation :dp[i]=max(dp[j]+1,1) (j<i&&str[j]<=str[i])
int result[len]; //result[i] Express str[i] The predecessor node of
result[0]=-1; // The first node of a sequence is set to -1, Distinguish from other nodes
for(int i=1;i<len;++i)
{
dp[i]=1;
result[i]=-1;
for(int j=0;j<i;++j)
{
if(str[j]<=str[i]&&dp[j]+1>dp[i])
{
dp[i]=dp[j]+1;
result[i]=j;
}
}
}
int p=0,imax=0;
for(int i=0;i<len;++i)
{
if(dp[i]>imax)
{
imax=dp[i];
p=i;
}
}
string lis;
for(;p!=-1;)
{
lis=str[p]+lis;
p=result[p];
}
cout << imax << endl;
cout << lis << endl;
cout << " debugging :\n";
for(auto a:str)
cout << a << ' ';
cout << endl;
for(auto a:dp)
cout << a << ' ';
cout << endl;
for(auto a:result)
cout << a << ' ';
cout << endl;
return 0;
}边栏推荐
- Leetcode minimum difference in student scores
- 记录一次压测经验总结
- Error: No named parameter with the name ‘foregroundColor‘
- Thread和Runnable创建线程的方式对比
- Time complexity & space complexity
- 使用知云阅读器翻译统计遗传学书籍
- ThinkPHP关联预载入with
- NiO related knowledge points (I)
- STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
- Using thread class and runnable interface to realize the difference between multithreading
猜你喜欢

PMP证书有没有必要续期?

AOSP ~Binder 通信原理 (一) - 概要

Leetcode(46)——全排列

Mysql database (basic)

A line of R code draws the population pyramid

【愚公系列】2022年7月 Go教学课程 005-变量

批量归一化(标准化)处理
![[Yugong series] go teaching course 005 variables in July 2022](/img/29/2bb30443e1e418556b5e08932f75b4.png)
[Yugong series] go teaching course 005 variables in July 2022
[email protected]映射关系问题"/>接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题

Error: No named parameter with the name ‘foregroundColor‘
随机推荐
If you ask me about R code debugging, I will tell you head, STR, help
U++4 interface learning notes
JS variable case
ServiceMesh主要解决的三大痛点
Batch normalization (Standardization) processing
2.证券投资基金的概述
IMS data channel concept of 5g vonr+
Common Oracle SQL statements
sublime使用技巧
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
2. Overview of securities investment funds
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
指针与数组在函数中输入实现逆序输出
Inventory host list in ansible (I wish you countless flowers and romance)
基于Bevy游戏引擎和FPGA的双人游戏
第一篇论文的写作流程
File upload vulnerability summary
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
Weebly移动端网站编辑器 手机浏览新时代
装饰器基础学习02