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 ;}

 

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.