UVA 111 History Grading
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 point to each event whose rank matches its correct rank
- 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 eventI in the correct 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 containn integers, indicating the correct chronological order of N events. The remaining lines would each consist ofn integers with each line representing a student ' s chronological ordering of the N events. All lines'll containn numbers in the range, with each number appearing exactly once per line, and with each Nu Mber 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
The main idea: the first number of N represents the number of elements in the data, then a standard answer, followed by the answers of other students, to find out the two series of the longest common sub-series, attention given to the answer is that the event is in the first few occurrences, for example: 2 3 4 1
That is, the corresponding 1th event occurs in the 2nd, 2nd event occurs in 3rd ... The conversion is 4 1 2 3.
Problem Solving Ideas:
D[I][J] =
0 (i = = 0 | | j = = 0)
D[I-1] [j-1] + 1 (a[i] = B[i])
Max (D[i-1][j], d[i] [j-1])
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int a[25], b[25], dp[25 ][25];int Main () {int n, t;while (scanf ("%d", &n) = = 1) {for (int i = 1; I <= n; i++) {scanf ("%d", &t); a[t] = i ;} while (scanf ("%d", &t) = = 1) {B[t] = 1;for (int i = 2; I <= n; i++) {scanf ("%d", &t); b[t] = i;} memset (DP, 0, sizeof (DP)), for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j + +) {if (A[i]! = B[j]) {Dp[i][j] = Max (Dp[i-1][j], dp[i][j-1]);} else {Dp[i][j] = dp[i-1] [j-1] + 1;}}} printf ("%d\n", Dp[n][n]);}} return 0;}
UVA 111 History Grading (DP preliminary application)