ZJU2136 Longest Ordered Subsequence

2024-08-26 08:32

本文主要是介绍ZJU2136 Longest Ordered Subsequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是一道经典的最长上升子序列问题,首先确定阶段和状态。然而每个阶段仅仅有一个状态,使用一位数组递推。
令 dp[i] 表示以 i 结尾的最长上升子序列的长度,则有

dp[i] = { max(dp[j]) + 1 | 0 <= j <i, seq[i] > seq[j] }

边界 dp[0] = 1 表示首个为结尾的最长串长度为1。

#define LOCAL
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;const int maxn = 1005;int main() {
#ifdef LOCALfreopen("input.txt", "r", stdin);
#endifint seq[maxn], dp[maxn], i, j;int kase; cin >> kase;while (kase--) {int N; cin >> N;for (i = 0; i < N; ++i) cin >> seq[i];memset(dp, 0, sizeof(dp));dp[0] = 1;for (i = 1; i < N; ++i) {dp[i] = 1;for (j = 0; j < i; ++j) {if (seq[j] < seq[i] && dp[j] + 1 > dp[i]) dp[i] = dp[j] + 1;}}int maxnum = 0;for (i = 0; i < N; ++i) {if (dp[i] > maxnum)maxnum = dp[i];}cout << maxnum << endl;if (kase != 0) cout << endl;}return 0;
}

分析如下代码片段:

        for (i = 1; i < N; ++i) {dp[i] = 1;for (j = 0; j < i; ++j) {if (seq[j] < seq[i] && dp[j] + 1 > dp[i]) dp[i] = dp[j] + 1;}}

第一个 for 循环从 1 到 N-1,计算从第二个到最后一个数字结尾的最长递增串长度,对应于转移方程中的 max(dp[j]),即遍历找到 dp 数组中 i 前面最大的且数字要小于 i 的,然后 +1。对应于 dp[i] = dp[j] + 1,已经讲得很明白了

这篇关于ZJU2136 Longest Ordered Subsequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1108060

相关文章

leetcode#32. Longest Valid Parentheses

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", wh

Android Ordered Broadcast 有序广播

代码设置IntentFilter: IntentFilter intentFilter = new IntentFilter();intentFilter.setPriority(15);intentFilter.addAction("shortcut.song.com.myapplication.MY_BROADCAST");intentFilter.addCategory

java常用算法之最长回文子串(Longest Palindromic Substring)

方法一:时间复杂度为O(n^3) public static String longestPalindrome1(String s) {int maxPalinLength = 0;String longestPalindrome = null;int length = s.length();// check all possible sub stringsfor (int i = 0; i

[LeetCode] 300. Longest Increasing Subsequence

题:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目 Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should

[LeetCode] 594. Longest Harmonious Subsequence

题:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目 We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactl

[LeetCode] 687. Longest Univalue Path

题:https://leetcode.com/problems/longest-univalue-path/description/ 题目 Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may no

[LeetCode] 524. Longest Word in Dictionary through Deleting

题:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ 题目大意 对s删除某些元素,使得删除后的s 为 d中的某个元素。 思路 trie 首先将 建立tire,然后是用dfs搜索,遍历s。 boolean dfs(TrieNode node,String s,StringBuilder cu

LeetCode - 32. Longest Valid Parentheses

32. Longest Valid Parentheses  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair