最长的交替字符串_最长的交替子序列

最长的交替字符串

Problem statement:

问题陈述:

Given a sequence of numbers you have to find out the longest alternating subsequence and print the length of the subsequence. A sequence is an alternating sequence when it will be maintain like (increasing) -> (decreasing) -> (increasing) -> (decreasing) or (decreasing) -> (increasing) -> (decreasing) -> (increasing).

给定一个数字序列,您必须找出最长的交替子序列并打印该子序列的长度。 序列在保持不变时是交替序列,例如(增加)->(减少)->(增加)->(减少)(减少)->(增加)->(减少)->(增加)

Input:
T Test case
T no. of input array along with their element no. N

E.g.
3

8
2 3 4 8 2 5 6 8
8
2 3 4 8 2 6 5 4
7
6 5 9 2 10 77 5

Constrain:
1≤ T ≤ 20
1≤ N ≤50
1≤ A[i] ≤50

Output:
Print the length of the longest alternating subsequence.

Example

T=3

Input:	
8
2 3 4 8 2 5 6 8 

Output:
4 (4, 8, 2, 5)

Input:
8
2 3 4 8 2 6 5 4

Output:
5 ( 4, 8, 2, 6, 4 )

Input:		
7
6 5 9 2 10 77 5

Output:
6 (6, 5, 9, 2, 10, 5 )

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 up(a) = the value at the index a of the increasing array and down(a) = the value at the index a of the decreasing array

up(a) =递增数组的索引a处的值, down(a) =递减数组的索引a处的值

To find out the length of the longest alternating sequence we will follow these steps,

为了找出最长的交替序列的长度,我们将遵循以下步骤,

  1. We take two new array one is an increasing array and another is decreasing array and initialize it 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 any element is less than the current element then,

    如果任何元素小于当前元素,

    up (index of current element) = down (index of the comparing element) + 1;

    向上(当前元素的索引)=向下(比较元素的索引)+1;

  3. If the element is greater than the current element then, down

    如果元素大于当前元素,则向下

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

    (当前元素的索引)=向上(比较元素的索引)+1;

C++ Implementation:

C ++实现:

#include 
using namespace std;

int find_length(int* arr, int n)
{
    int up[n];
    int down[n];

    for (int i = 0; i < n; i++) {
        up[i] = 1;
        down[i] = 1;
    }

    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (arr[i] > arr[j]) {
                up[i] = max(up[i], down[j] + 1);
            }
            else if (arr[i] < arr[j]) {
                down[i] = max(down[i], up[j] + 1);
            }
        }
    }

    int m = 0;

    for (int i = 0; i < n; i++) {
        m = max(m, max(up[i], down[i]));
    }

    return m;
}

int main()
{
    //code
    int t;

    cout << "Testcase : ";
    cin >> t;

    while (t--) {
        int n;

        cout << "Enter the element number : ";
        cin >> n;

        int arr[n];

        cout << "Fill the array : ";
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }

        cout << "Length of the subsequence : " << find_length(arr, n) << endl;
    }

    return 0;
}

Output

输出量

Testcase : 3
Enter the element number : 8
Fill the array : 2 3 4 8 2 5 6 8	
Length of the subsequence : 4
Enter the element number : 8
Fill the array : 2 3 4 8 2 6 5 4
Length of the subsequence : 5
Enter the element number : 7
Fill the array : 6 5 9 2 10 77 5
Length of the subsequence : 6


翻译自: https://www.includehelp.com/icp/longest-alternating-subsequence.aspx

最长的交替字符串

你可能感兴趣的:(最长的交替字符串_最长的交替子序列)