UVA 111 history Grading (longest common sub-sequence)

Source: Internet
Author: User


History GradingTime limit : MS Memory Limit:0KB 64bit IO Format:% LLD &%llu
Background

Many problems in computer science involve maximizing some measure according to constraints.

Consider a history exam in which students is asked to put several historical events into chronological order. Students who order all the events correctly would receive full credits, but how should partial credits is awarded to Students Who incorrectly rank one or more of the historical events?

Some possibilities for partial credits include:

    1. 1 point to each event whose rank matches its correct rank
    2. 1 point for each event in the longest (not necessarily contiguous) sequence of events which is in the correct order Relat Ive to all other.

For example, if four events is correctly ordered 1 2 3 4 then the Order 1 3 2 4 would receive a score of 2 using the Firs T method (events 1 and 4 are correctly ranked) and a score of 3 using the second method (event sequences 1 2 4 and 1 3 4 A Re both in the correct order relative.

In this problem you is asked to write a program to score such questions using the second method.

The problem

Given the correct chronological order of N events as where denotes the ranking of event I in the Corr ECT chronological order and a sequence of student responses where denotes the chronological rank given by the student to Event I; Determine the length of the longest (not necessarily contiguous) sequence of events in the student responses that is in the correct chronological order relative to all other.

The Input

The first line of the input would consist of one integer n indicating the number of events with. The second line would contain nintegers, indicating the correct chronological order of N events. The remaining lines would each consist of n integers with each line representing a student ' s chronological orderin G of the N events. All lines'll contain n numbers in the range, with each number appearing exactly once per line, and with each Number separated from and numbers on the same line by one or more spaces.

The Output

For each student ranking of events your program should print the score for that ranking. There should is one line of output for each student ranking.

Sample Input 1

44 2 3 11 3 2 43 2 1 42 3 4 1

Sample Output 1

123

Sample Input 2

103 1 2 4 9 5 10 6 8 71 2 3 4 5 6 7 8 9 104 7 2 3 10 6 9 1 5 83 1 2 4 9 5 10 6 8 72 10 1 3 8 4 9 5 7 6


Sample Output 2

65109


This problem, needless to say, the longest common sub-sequence

At first, see only the second rule to solve, think of is the common sub-sequence, and then began to knock code, the longest common sub-sequence of the code to knock Good, run, test, in the Sample Input1 results exactly match output, submit, wa~~~~~~, is feeling depressed, began to find the wrong, Read a few times the code to determine the longest common subsequence write Yes, and then test Sample Input2 This set of data, the results even test data have not been, feel more depressed, come back to see the second, yes Ah, is the longest common sub-sequence ah, and then began to re-read the question, to see if it is wrong to understand which point , I read the question two times with a blindfold, still did not find anything (bad English is flawed AH), and then continue to read, and then read for a while, and thought, finally found that the order he gave is that the first event is in the number of occurrences, as in the previous sort of problem,,, alas:-(, to kneel, can be such a son, And then changed the code, submit, AC, (⊙o⊙) Oh, it's not easy ah, a longest common sub-sequence of living written for two hours ~~~~~~~~~~~~~ slowly are tears ah ~~~~~~~~~~~~~~!

Test instructions probably says, give you 1 to n events, sorted by the sequence of the first row and the longest common subsequence after sorting by the other line sequence.



Attached code:


#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include < string> #include <queue> #define INF 0x3f3f3f3f#define max_n 100using namespace Std;int num[max_n];int Dp[max_n] [Max_n];int Main () {int n;cin >> n;for (int i = 1;i <= n;i++) {int k;cin >> k;num[k] = i;          The  }int a[max_n];int C;memset (dp,0,sizeof (DP)) is arranged in the order given, while (CIN >> c) {A[c] = 1;for (int i = 2;i <= n;i++) {C In >> c;a[c] = i;   }for (int i = 1;i <= n;i++)/DP in the order given is the longest        common subsequence {for (int j = 1;j <= n;j++) {if (num[i] = = A[j]) {dp[i + 1][ J + 1] = Dp[i][j] + 1;} Elsedp[i + 1][j + 1] = max (dp[i][j + 1],dp[i + 1][j]);}} cout << dp[n + 1][n + 1] << Endl;} return 0;}


UVA 111 history Grading (longest common sub-sequence)

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.