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

最长的交替字符串

Problem statement:

问题陈述:

Given a sequence of numbers you have to print the longest alternating subsequence. A sequence is an alternating sequence when it will be maintaining 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 longest alternating subsequence.

Example

T=3

Input:
8
2 3 4 8 2 5 6 8 
Output:
4 8 2 5

Input:
8
2 3 4 8 2 6 5 4
Output:
4 8 2 6 4 

Input:
7
6 5 9 2 10 77 5
Output:
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.

    up(当前元素的索引)= down(比较元素的索引)+1

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

    如果元素大于当前元素,

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

    down(当前元素的索引)= up(比较元素的索引)+1

  4. After that, we traverse the two arrays and take the maximum value.

    之后,我们遍历两个数组并取最大值。

Length of the subsequence= max (up(i), down(i))

To print the longest increasing odd-even subsequence, we will follow these steps,

要打印最长的增加的奇偶子序列,我们将按照以下步骤操作,

  1. We find out the index where the max value contains.

    我们找出最大值所在的索引。

  2. If the element at that index is in up array then we traverse the down array where the value difference is one.

    如果该索引处的元素在上数组中,则我们遍历下数组中值差为1的数组。

  3. If the element at that index is in down the array then we traverse the up array where the value difference is one.

    如果该索引处的元素在数组的下移,则我们遍历值差为1的上一数组。

  4. Follow step 2 and 3 until the value will be 1.

    继续执行步骤2和3,直到该值为1。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

void print_subsequence(int* arr, int* up, int* down, int n, int m, int ind)
{
    int f_up = 0;
    int f_down = 0;
    vector<int> v;

    if (m == up[ind]) {
        f_up = 1;
    }
    else {
        f_down = 1;
    }

    v.push_back(arr[ind]);
    for (int i = ind - 1; i >= 0; i--) {
        if (m == 1) {
            break;
        }
        if (f_down) {
            if (up[i] + 1 == m) {
                v.push_back(arr[i]);
                m = up[i];
                f_down = 0;
                f_up = 1;
            }
        }
        else if (f_up) {
            if (down[i] + 1 == m) {
                v.push_back(arr[i]);
                m = down[i];
                f_down = 1;
                f_up = 0;
            }
        }
    }
    for (int i = v.size() - 1; i >= 0; i--) {
        cout << v[i] << " ";
    }
    cout << endl;
}

void 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;
    int ind = 0;

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

    print_subsequence(arr, up, down, n, m, ind);
}

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);
    }
    
    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 8 2 5 
Enter the element number : 8
Fill the array : 2 3 4 8 2 6 5 4
Length of the subsequence : 4 8 2 6 5 
Enter the element number : 7              
Fill the array : 6 5 9 2 10 77 5
Length of the subsequence : 6 5 9 2 77 5


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

最长的交替字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值