What is a bigger smarter? Longest Common subsequence

Source: Internet
Author: User

Question: The weight (IQ) of up to 1000 elephants may be the same, and the weight may be the same, the length and sequence of the strictly decreasing IQ are output. The sequence is represented by the serial number in the input data.

Idea: Step 1: sort these elephants by weight. During sorting, you only need to sort the indexes. You do not need to change the actual data of the elephants, such as (600,300,400 ), the corresponding index is (1, 2, 3) followed by (2, 3, 1 ).

Step 2: Sort IQ, which eliminates duplicates. I remember seeingAlgorithmHow do I sort several integers from 0 to N by the time of O (n? My answer is: use an array appear [N] and initialize it to 0. If the number M appears, appear [m] ++ and copy the last loop to an array. In addition, this can easily eliminate duplicates.

Step 3: Calculate the longest common subsequence for the above two sequences. Fill in the subproblem form. Find the optimal solution.

Finally, remove duplicate weights in the sequence and output them.

Code:

 

  # Include  <  Cstdio  > 
# Include < Algorithm >
Using Namespace STD;

# Define Maxcompute 1000

Struct Elephant {
Int W;
Int S;
};

Int N, m;
Elephant elephants [Max + 1 ];
Int A [Max + 1 ]; // Index
Int B [Max + 1 ]; // Sort elephant IQ in descending order
Bool Appear [ 10000 + 1 ]; // Used to sort the IQ of an elephant. If an elephant with an IQ of I appears, set in [I] to true.
Int Sub [Max + 1 ], Len; // Sub-sequence, Length

Int DP [Max + 1 ] [Max + 1 ];
Char Sel [Max + 1 ] [Max + 1 ]; // When DP is selected, 1 indicates equal, 2 left, 3 up

// Function object used to sort Weights
Struct CMP {
Bool Operator ()( Int A, Int B ){
Return (ELEPHANTS [A]. W < Elephants [B]. W );
}
};

// Longest Common subsequence
Void LCS (){
Int I, J;
For (I = 0 ; I <= N; ++ I)
DP [I] [ 0 ] = 0 ;
For (J = 0 ; J <= M; ++ J)
DP [ 0 ] [J] = 0 ;
For (I = 1 ; I <= N; ++ I)
For (J = 1 ; J <= M; ++ J ){
If (ELEPHANTS [A [I]. s = B [J]) {
DP [I] [J] = DP [I - 1 ] [J - 1 ] + 1 ;
Sel [I] [J] = 1 ;
}
Else If (DP [I - 1 ] [J] <= DP [I] [J - 1 ]) {
DP [I] [J] = DP [I] [J - 1 ];
Sel [I] [J] = 2 ;
}
Else {
DP [I] [J] = DP [I - 1 ] [J];
Sel [I] [J] = 3 ;
}
}
}

// Trace back to common subsequences
Void Backtrace (){
Len = 0 ;
Int I, J;
I = N, J = M;
While (I ! = 0 && J ! = 0 ){
If (SEL [I] [J] = 1 ){
++ Len;
Sub [Len] = A [I];
-- I, -- J;
}
Else If (SEL [I] [J] = 2 )
-- J;
Else
-- I;
}
}

Int Main (){
// Freopen ("in", "r", stdin );
Int W, S;
N = 0 ;
While (Scanf ( " % D " , & W, & S) ! = EOF ){
++ N;

Elephants [N]. W = W;
Elephants [N]. s = S;
Appear [s] = True ;
A [n] = N;
}
Sort ( + 1 , + 1 + N, CMP ());

M = 0 ;
For ( Int I = 10000 ; I > = 1 ; -- I ){
If (Appear [I]) {
M ++ ;
B [m] = I;
}
}
LCS ();
Backtrace ();

// Remove the same weight
Int K, J;
K = 1 ;
B [ 1 ] = Sub [Len];
For (J = Len - 1 ; J > = 1 ; -- J ){
If (ELEPHANTS [sub [J]. W = Elephants [B [K]. W)
-- Len;
Else {
++ K;
B [k] = Sub [J];
}
}
Printf ( " % D \ n " , Len );
For (K = 1 ; K <= Len; ++ K)
Printf ( " % D \ n " , B [k]);

Return 0 ;
}

 

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.