日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

最长递增子序列 子串_最长递增奇偶子序列

發(fā)布時間:2025/3/11 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最长递增子序列 子串_最长递增奇偶子序列 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最長遞增子序列 子串

Problem statement:

問題陳述:

Given a sequence of numbers you have to find out the length of the longest increasing odd even subsequence and print the length of the subsequence. The sequence will be maintaining like, (odd ) -> ( even ) -> ( odd ) -> ( even ) or (even ) -> ( odd ) -> ( even ) -> ( odd ) .

給定一個數(shù)字序列,您必須找出最長的遞增奇數(shù)偶數(shù)子序列的長度并打印該子序列的長度。 順序?qū)⒈3譃?odd)->(even)->(奇數(shù))->(偶數(shù))或(even)->(奇數(shù))->(偶數(shù))->(奇數(shù)) 。

Input: T Test case T no. of input array along with their element no. NE.g. 38 2 3 4 8 2 5 6 88 2 3 4 8 2 6 5 47 6 5 9 2 10 77 5Constrain: 1≤ T ≤ 20 1≤ N ≤50 1≤ A[i] ≤50Output: Print the length of the longest increasing odd even subsequence

Example

T=3Input: 8 2 3 4 8 2 5 6 8 Output: 5 ( 2 3 4 5 8 )Input: 8 2 3 4 8 2 6 5 4 Output: 4 ( 2 3 4 5 )Input: 7 6 5 9 2 10 77 5Output: 4 (6 9 10 77 )

Explanation with example:

舉例說明:

Let N be the number of elements say, X1, X2, X3 ... Xn.

令N為元素數(shù),即X 1 ,X 2 ,X 3 ... X n 。

Let odd(a) = the value at the index a of the odd array and even(a) = the value at the index a of the even array.

令odd(a) =奇數(shù)數(shù)組的索引a處的值,而even(a) =偶數(shù)數(shù)組的索引a處的值。

To find the length of the longest increasing odd even subsequence we will follow these steps,

要找到最長的遞增奇數(shù)偶數(shù)子序列的長度,我們將按照以下步驟操作,

  • We take two new array one is an odd array and another is even an array and initialize both with 1. We start our algorithm with the second column. We check elements that are before the current element, with the current element.

    我們采用兩個新的數(shù)組,一個是奇數(shù)數(shù)組,另一個是偶數(shù)數(shù)組,并都用1初始化。我們從第二列開始我們的算法。 我們使用當(dāng)前元素檢查當(dāng)前元素之前的元素。

  • If the current element is odd and the comparing the element is even then,

    如果當(dāng)前元素為奇數(shù),而比較元素為偶數(shù),

    odd (index of current element) = even (index of the comparing element) + 1 ;

    奇數(shù)(當(dāng)前元素的索引)=偶數(shù)(比較元素的索引)+1;

  • If the current element is even and the comparing element is odd then,

    如果當(dāng)前元素為偶數(shù),而比較元素為奇數(shù),

    even (index of current element) = odd (index of the comparing element) + 1;

    偶數(shù)(當(dāng)前元素的索引)=奇數(shù)(比較元素的索引)+1;

  • C++ Implementation:

    C ++實現(xiàn):

    #include <bits/stdc++.h> using namespace std;int length_of_subsequence(int* arr, int n) {int a[n];for (int i = 0; i < n; i++) {a[i] = 1;}for (int i = 1; i < n; i++) {for (int j = 0; j < i; j++) {if (arr[i] % 2 == 0) {if (arr[j] % 2 == 1 && arr[j] < arr[i]) {a[i] = max(a[i], a[j] + 1);}}else {if (arr[j] % 2 == 0 && arr[j] < arr[i]) {a[i] = max(a[i], a[j] + 1);}}}}int Max = 0;for (int i = 0; i < n; i++) {Max = max(Max, a[i]);}cout << endl;return Max; }int main() {int t;cout << "TestCase : ";cin >> t;while (t--) {int n;cout << "Enter number of elements : ";cin >> n;int arr[n];cout << "Enter the elements : ";for (int i = 0; i < n; i++) {cin >> arr[i];}cout << "Length of the subsequence : " << length_of_subsequence(arr, n) << endl;}return 0; }

    Output

    輸出量

    TestCase : 3 Enter number of elements : 8 Enter the elements : 2 3 4 8 2 5 6 8 Length of the subsequence : 5 Enter number of elements : 8 Enter the elements : 2 3 4 8 2 6 5 4 Length of the subsequence : 4 Enter number of elements : 7 Enter the elements : 6 5 9 2 10 77 5 Length of the subsequence : 4

    翻譯自: https://www.includehelp.com/icp/longest-increasing-odd-even-subsequence.aspx

    最長遞增子序列 子串

    總結(jié)

    以上是生活随笔為你收集整理的最长递增子序列 子串_最长递增奇偶子序列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。