Code for the C ++ Implementation of the longest ordered substring

Source: Internet
Author: User

// The difference between a sub-string and a sub-sequence is that the sub-string is continuous and the sub-sequence can be discontinuous.

# Include <iostream>
# Include <vector>

Using namespace std;

Void MaxOrderedSub (const vector <int> & data)
{
Int num = data. size ();
// Use an auxiliary array to record the length of the oldest string ending with I
Vector <int> array (num, 1 );
For (int I = 1; I <num; I ++)
{
If (data [I]> data [I-1])
Array [I] = array [I-1] + 1;
}

Int result = 0;
Int last = 0;

// Find the maximum value in array, which is the longest sequence length.
// Last is the subscript in the array, that is, the end point of the longest sequence.
For (int k = 0; k <num; k ++)
{
If (array [k]> result)
{
Result = array [k];
Last = k;
}
}

Cout <"the long of the sequence:" <result <endl;
Cout <"the sequence:" <endl;

For (int j = last; array [j]! = 1; j --);
For (I = j; I <= last; I ++)
Cout <data [I] <"";
Cout <endl;
}

Int main ()
{
Cout <"input the size of the array:" <endl;
Int num;
Cin> num;
Vector <int> arr (num, 0 );
For (int I = 0; I <num; I ++)
Cin> arr [I];
MaxOrderedSub (arr );
Return 1;
}

Author: "The column of the catalogue with big smoke"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.