Longest DP public sub-sequence (ultraviolet) 111-history grading

Source: Internet
Author: User

 

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 47

 

Question meaning:

Give the correct time table for 1-N events and the time table for 1-N events to calculate the maximum number of events, which is the same as the correct order.

 

Solution:

Sort Events in order of ranking, and then find the length of the longest common subsequence.

DP [I] [J] = DP [I-1] [J-1] + 1 (save1 [I] = save [J]) DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]);

 

Code:

# Include <iostream>
# Include <cmath>
# Include <cstdio>
# Include <cstdlib>
# Include <string>
# Include <cstring>
# Include <algorithm>
# Include <vector>
# Include <map>
# Include <stack>
# Include <queue>
# Define EPS 1e-6
# Define Inf (1 <20)
# Define PI ACOs (-1.0)
Using namespace STD;

Int save1 [30], save2 [30];
Int DP [30] [30];

Int main ()
{
Int N, temp;

Scanf ("% d", & N );
For (INT I = 1; I <= N; I ++)
{
Scanf ("% d", & temp );
Save1 [temp] = I; // sort events in the order of ranking, so that the longest common subsequence has a relatively same ranking
}

While (scanf ("% d", & temp )! = EOF)
{
Save2 [temp] = 1;
For (INT I = 2; I <= N; I ++)
{
Scanf ("% d", & temp );
Save2 [temp] = I;
}
Memset (DP, 0, sizeof (DP ));
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= N; j ++)
{
If (save1 [I] = save2 [J])
DP [I] [J] = DP [I-1] [J-1] + 1; // max (DP [I-1] [J-1] + 1, DP [I-1] [J], DP [I] [J-1]);
Else
DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]);
}
Printf ("% d \ n", DP [N] [N]);

}

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.