uva111 (复习dp, 14.07.09)

 History Grading 

Background

Many 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 should partial
credit be awarded to students who incorrectly rank one or more of the historical events?

Some possibilities for partial credit include:

  1. 1 point for each event whose rank matches its correct rank
  2. 1 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 would receive a score of 2 using the first 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 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 Problem

Given 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 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 are in the correct chronological order relative to each other.

The Input

The first line of the input will consist of one integer n indicating the number of events with  .
The second line will contain nintegers, 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 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 Output

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

Sample Input 1

4
4 2 3 1
1 3 2 4
3 2 1 4
2 3 4 1

Sample Output 1

1
2
3

Sample Input 2

10
3 1 2 4 9 5 10 6 8 7
1 2 3 4 5 6 7 8 9 10
4 7 2 3 10 6 9 1 5 8
3 1 2 4 9 5 10 6 8 7
2 10 1 3 8 4 9 5 7 6

Sample Output 2

6
5
10
9

求最长公共子序列

AC代码

#include<stdio.h>
#include<string.h>

int max(int a, int b) {
	if(a > b)
		return a;
	else
		return b;
}

int main() {
	int l;
	int t;
	int num1[100];

	scanf("%d", &l);
	for(int i = 1; i <= l; i++) {
		scanf("%d", &t);
		num1[t] = i;
	}

	int num2[100];
	while(scanf("%d", &t) != EOF) {
		num2[t] = 1;
		for(int i = 2; i <= l; i++) {
			scanf("%d", &t);
			num2[t] = i;
		}

		int dp[100][100];
		memset(dp, 0, sizeof(dp));

		for(int i = 1; i <= l; i++) {
			for(int j = 1; j <= l; j++) {
				if(num1[i] == num2[j])
					dp[i][j] = dp[i-1][j-1] + 1;
				else
					dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
			}
		}

		printf("%d\n", dp[l][l]);
	}

	return 0;
}

uva111 (复习dp, 14.07.09)

时间: 2024-08-05 11:45:38

uva111 (复习dp, 14.07.09)的相关文章

uva 674 (入门DP, 14.07.09)

 Coin Change  Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money. For example, if we have 11 cents, then we can make changes with one 10-cent coin an

HDU 1528 (二分图最大匹配 + 最小覆盖, 14.07.17)

Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the c

张珺 2015/07/09 个人文档

姓名 张珺 日期 中蓝公寓蓝芳园D507,2015/07/09 主要工作及心得 在今天的设计工作中, 我完成了操作者界面中12个界面的设计工作.并参与了部分代码的合并工作. 通过今天对界面的设计工作,我学会了Java中界面设计的方法,以及相应控件的使用方式. 遇到的问题 由于之前对于Java中界面设计并没有深入学习,对于界面之间的跳转和空间的使用也不太了解,设计时在这方面遇到了一些问题. 解决方法 因为经理.提供者等界面的编写工作已由其他同学完成,因此,遇到问题的大部分问题都可以在同伴的帮助下解

07 09&amp;10

0709: 排名还是不高,毕竟没切出来题. 第一题dalao: 要求你做一个三维数点,只回答最终有多少个点对的状态是完全小于(可比?)的.(n<=2000000) 特殊限制是三维都是随机排列. 陈立杰在APIO讲过,当时我翘了...... 在考场上努力卡常,结果卡常失败,应该拿到60,评测炸了,中间奇怪的RE,所以只有40. 正解是考虑一对点对(X,Y),由于有三维的对应大小,所以对应的大小关系只有 (2,1)(1,2)(3,0)(0,3)这几种, 我们要求的是所有(3,0)(0,3)的点对数目

2017.07.09【NOIP提高组】模拟赛B组

Summary 今天放假,比赛于是就没有打了,但是看了一下题,发现都挺简单了,不想码~╮(╯▽╰)╭懒虫一条.最后一题居然做过原题.这次比赛让我对并查集“刮目相看”,对贪心感到“前途无量”,觉得树形DP很有趣,但也很难,但也都打出来了.要多做关于树形DP的题目,来充实自己. Problem T1 [GDOI2003]购物 题目大意 给你很多种物品,你购买每个物品就可以剩下ai钱,但是编号x和y号物品,买了其一,就不能买其二,且不会出现一个环的情况.问最多能节省多少钱. 想法 这道题好像做过一道类

.Net学习笔记----2015-07-10(基础复习和练习07)

为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩. static void Main(string[] args) { int[] score = new int[30]; Random r = new Random(); //int rNumber = r.Next(1, 101); //double sum = 0.00; //for (int i = 0; i < score.Length; i++) //{ //

iOS复习笔记14:常用数据结构之类

一 NSString/NSMutableString字符串 1 NSString <pre name="code" class="objc">NSString* s1 = @"string"; //NSString* s2 = [[NSString alloc] initWithFormat(@"%d is one",1)]; NSString* s2 = [NSString stringWithFormat(@&

陈嘉 2015/07/09 个人文档

姓名 陈嘉 日期 2015/7/9 主要工作及心得 昨天完成了数据传输部分的代码.今天开始了和界面部分的代码合并.因为前期接口做得比较好,我在代码中用中文写了需要从界面获取的数据,合并的时候直接将相应的数据放入相应的地方,所以合并工作就比较顺利.这就体现了接口的重要性.好的接口可以让各模块交互更为方便. 我的代码获取是按照功能划分的,但是实际情况有一些同一个功能需要获取的数据分在了两个页面上,这就需要数据传输,函数传参. 遇到的问题 客户端部分代码编写是按照功能划分的,和界面代码编写有一些不对应

2016.07.09

javascript 中 offsetWidth 是对象的可见宽度,包滚动条等边线,会随窗口的显示大小改变 clientWidth.offsetWidth.clientHeight区别 IE6.0.FF1.06+: offsetWidth = width + padding + border offsetHeight = height + padding + border IE5.0/5.5: offsetWidth = width offsetHeight = height offsetwid