The longest zig-zag subsequence
类似于最长不降子序列。
找到最长的大小相间的子序列。
dp[k] 表示一个结构体,以k号结尾的最长zig-zag子序列长度,和期待下一个数是大还是小。
struct node{int len, state; //state, 0表示下一个数必须大,1表示必须小,2表示都可以
}
状态转移方程
dp[k].len = max( num[k]能接到dp[i]后面 ? dp[i].len+1 : 1 ), 0<=i<=k-1
边界
dp[0].len = 1;
dp[0].state = 2;
处理顺序
从左往右
#include <cstdio>struct node{int len, state;
};int main(){int n, ans = 1;scanf("%d", &n);int num[n];for(int i=0; i<n; i++) scanf("%d", &num[i]);node dp[n];dp[0].len = 1;dp[0].state = 2;for(int i=1; i<n; i++){dp[i].len = -1;for(int j=0; j<i; j++){node temp;if(dp[j].state==0){if(num[i]>num[j]){temp.len = dp[j].len+1;temp.state = 1;}else{temp.len = 1;temp.state = 2;}}else if(dp[j].state==1){if(num[i]<num[j]){temp.len = dp[j].len+1;temp.state = 0;}else{temp.len = 1;temp.state = 2;}}else{if(num[i]>num[j]){temp.len = dp[j].len + 1;temp.state = 1;}else if(num[i]<num[j]){temp.len = dp[j].len + 1;temp.state = 0;}else{temp.len = 1;temp.state = 2;}}if(temp.len > dp[i].len) dp[i] = temp;}if(dp[i].len > ans) ans = dp[i].len;}printf("%d", ans);return 0;
}
The longest zig-zag subsequence相关推荐
- LintCode Longest Increasing Continuous Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/ 题目: Give an in ...
- 674 Longest Continuous Increasing Subsequence(每日一题)
674:Longest Continuous Increasing Subsequence 每日一题的第一天 解题思路 双重循环 class Solution {public int findLeng ...
- LeetCode 674. Longest Continuous Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
- C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- LeetCode 673. Number of Longest Increasing Subsequence--O(N log N )--Java,C++,Python解法
题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...
- LeetCode 674. Longest Continuous Increasing Subsequence--python,Java,C++解法
此题链接:Longest Continuous Increasing Subsequence - LeetCode Given an unsorted array of integers, find ...
- 九章算法班ladder题目梳理
1 - 从strStr谈面试技巧与代码风格 13.字符串查找 如果target在source中,返回起始下标,否则返回-1 要点:该题O(mn)可过,两层循环即可. class Solution:de ...
最新文章
- python安装出错0xc00006b_Python安装时我遇到的一些问题
- SqlServer 0和空字符串''等价?-----类型的隐式转换问题
- 《我的侠客》主策划何铮:如何打出武侠游戏的新招
- mysql as用法_Spring Data R2DBC响应式操作MySQL
- EBS MOAC深入研究(转)
- UIViewController内存管理
- Q140:PBRT-V3,各种渲染算法(Integrator,积分器)汇总
- SQL调优技巧:统计信息(文末福利)
- pythonsuper多重继承_解决python super()调用多重继承函数的问题
- 小程序 实名信息_公告丨DUNK PLUS 小程序实名认证开启
- 麻雀算法(SSA)优化的BP神经网络预测,SSA-BP回归预测,多输入单输出模型。
- 吐槽一下程序员职场那些令人迷惑的行为
- 2022R2移动式压力容器充装考题模拟考试平台操作
- 记录使用4G模块SIM7600CE的一些问题
- 手势识别原来还可以这样控制电脑,你知道怎么做到的吗?
- 线性规划模型应用的2个案例
- AQS是什么?都是怎么用的?
- saas系统的微服务器,基于微服务架构的SaaS服务应用的研究与实践
- 鲁班H5页面生成工具
- 回顾我在中兴通讯的五年(一)