博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最长递增子序列 子串_最长递增奇偶子序列
阅读量:2533 次
发布时间:2019-05-11

本文共 3312 字,大约阅读时间需要 11 分钟。

最长递增子序列 子串

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 ) .

给定一个数字序列,您必须找出最长的递增奇数偶数子序列的长度并打印该子序列的长度。 顺序将保持为(odd)->(even)->(奇数)->(偶数)(even)->(奇数)->(偶数)->(奇数)

Input:T Test caseT no. of input array along with their element no. NE.g.382 3 4 8 2 5 6 882 3 4 8 2 6 5 476 5 9 2 10 77 5Constrain:1≤ T ≤ 201≤ N ≤501≤ A[i] ≤50Output:Print the length of the longest increasing odd even subsequence

Example

T=3Input:82 3 4 8 2 5 6 8 Output:5 ( 2 3 4 5 8  )Input:82 3 4 8 2 6 5 4Output:4 ( 2 3 4 5 )Input:76 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为元素数,即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) =奇数数组的索引a处的值,而even(a) =偶数数组的索引a处的值。

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

要找到最长的递增奇数偶数子序列的长度,我们将按照以下步骤操作,

  1. 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.

    我们采用两个新的数组,一个是奇数数组,另一个是偶数数组,并都用1初始化。我们从第二列开始我们的算法。 我们使用当前元素检查当前元素之前的元素。

  2. If the current element is odd and the comparing the element is even then,

    如果当前元素为奇数,而比较元素为偶数,

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

    奇数(当前元素的索引)=偶数(比较元素的索引)+1;

  3. If the current element is even and the comparing element is odd then,

    如果当前元素为偶数,而比较元素为奇数,

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

    偶数(当前元素的索引)=奇数(比较元素的索引)+1;

C++ Implementation:

C ++实现:

#include 
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 : 3Enter number of elements : 8Enter the elements : 2 3 4 8 2 5 6 8Length of the subsequence : 5Enter number of elements : 8Enter the elements : 2 3 4 8 2 6 5 4Length of the subsequence : 4Enter number of elements : 7Enter the elements : 6 5 9 2 10 77 5Length of the subsequence : 4

翻译自:

最长递增子序列 子串

转载地址:http://cxtzd.baihongyu.com/

你可能感兴趣的文章
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
点语法
查看>>
IO之Socket网络编程
查看>>
PCRE demo【转】
查看>>
矩阵的SVD分解
查看>>
gitlab的配置
查看>>
linux访问ftp服务器命令
查看>>
ActiveMQ学习笔记之异常
查看>>
LuoguP4012 深海机器人问题(费用流)
查看>>
自动机
查看>>
java知识点
查看>>
WPF设计の画刷(Brush)
查看>>
HTML学习笔记
查看>>
selinux详解及配置文件
查看>>
性能优化之数据库优化
查看>>
Swift - 继承UIView实现自定义可视化组件(附记分牌样例)
查看>>
android自定义viewgroup实现等分格子布局
查看>>