Ultraviolet A 111: History grading

Source: Internet
Author: User

First, we need to deal with the input. The general idea of solving the problem is to rearrange the given C array and R array according to the rank of each historical event, that is, the number of the earliest event is placed in the first place of the array ...... then this question is transformed into the question of finding the longest common sub-sequence length of two strings.

However, I used another solution (although dynamic planning is still used =-= ):

Only sorts the input C array (that is, the serial number of the event where the rank is I in the C array), and the r array remains unchanged. Create an ans array. ans [I] stores the longest sequence length ending with rank I, and the initialization value is 1.

The program fills the ANS array starting from 0th. When executed to evaluate ans [I], respectively judge rank events from 0-i-1, such as J events, in the student's answer (that is, the data in the r array) if the time before the event is also before the I event, use ans [J] + 1 to update ans [I] (because ans [I] is initialized to 1 ). After the ANS array is filled, the maximum value is the result.

My code is as follows:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <string> # include <algorithm> using namespace STD; int C [20], R [20]; int ans [20]; int N; int main () {CIN> N; int ci; For (INT I = 0; I <n; I ++) {// rearrange the C array CIN in chronological order> Ci; C [ci-1] = I;} int TMP; while (CIN> TMP) {R [0] = TMP; ans [TMP] = 0; For (INT I = 1; I <n; I ++) CIN> r [I]; int maxlen = 1; ans [0] = 1; for (INT I = 1; I <n; I ++) {ans [I] = 1; // judge Disconnection event 0 ~ I-1for (Int J = 0; j <I; j ++) {If (R [C [J] <R [C [I]) {If (ANS [I] <(ANS [J] + 1) ans [I] = ans [J] + 1 ;}} if (maxlen <ans [I]) maxlen = ans [I];} cout <maxlen <Endl;} 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.