UV 111 History Grading (dp + LCS)

Source: Internet
Author: User

Background1_problems in Computer Science involve maximizing some measure according to constraints. consider a history exam in which students are asked to put several historical events into chronological order. students who order all the events correctly will receive full credit, but how shoshould partial credit be awarded to students who incorrectly rank one or more of the historical events? Some possibilities for partial credit include: 1 point for each event whose rank matches its correct rank1 point for each event in the longest (not necessarily contiguous) sequence of events which are in the correct order relative to each other. for example, if four events are correctly ordered 1 2 3 4 then the order 1 3 2 4 wowould receive a score of 2 using the first method (events 1 and 4 are correc Tly ranked) and a score of 3 using the second method (event sequences 1 2 4 and 1 3 4 are both in the correct order relative to each other ). in this problem you are asked to write a program to score such questions using the second method. the ProblemGiven the correct chronological order of n events as where denotes the ranking of event I in the correct chronological order and a sequence of student r Esponses 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 are in the correct chronological order relative to each other. the InputThe first line of the input will consist of one integer n indicating the number of events. the second line will contain n integers, Indicating the correct chronological order of n events. the remaining lines will each consist of n integers with each line representing a student's chronological ordering of the n events. all lines will contain in n numbers in the range, with each number appearing exactly once per line, and with each number separated from other numbers on the same line by one or more spaces. the OutputFor each stud Ent ranking of events your program shocould print the score for that ranking. there shoshould be one line of output for each student ranking. sample Input 144 2 3 11 3 2 43 2 1 42 3 4 1 Sample Output 1123 Sample Input 2103 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 6 Sample Output 265109 question: for the sequence of some historical events, the same sequence is required. Idea: Longest Common subsequence problem. Note that an event is entered in the question. So num1 [sb] = I is required. In this way, the order of occurrence is stored. Code:

#include <stdio.h>#include <string.h>int n, num1[25], num2[25], i, j, d[25][25], sb;int max(int a, int b) {return a > b ? a : b;}int main() {scanf("%d", &n);for (i = 1; i <= n; i ++) {scanf("%d", &sb);num1[sb] = i;}while (~scanf("%d", &sb)) {num2[sb] = 1;memset(d, 0, sizeof(d));for (i = 2; i <= n; i ++) {scanf("%d", &sb);num2[sb] = i;}for (i = 1; i <= n; i ++)for (j = 1; j <= n; j ++) {if (num1[i] == num2[j])d[i][j] = d[i - 1][j - 1] + 1;elsed[i][j] = max(d[i - 1][j], d[i][j - 1]);}printf("%d\n", d[n][n]);}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.