UVA 111 (复习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 point for each event whose rank matches its correct rank
- 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;
}
UVA 111 (复习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 ...
- UVA 111 简单DP 但是有坑
题目传送门:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18201 其实是一道不算难的DP,但是搞了好久,才发现原来是题目没 ...
- 2019.07.09 纪中_B
错失AK记 2019.07.09[NOIP提高组]模拟 B 组 明明今天的题都很水,可就是没蒟蒻. 写题的时候: T0一眼高精(结果没切)T1看到2啊8啊果断转二进制观察,发现都是左移几位然后空出的位 ...
- LEETCODE 07 09
最近忙着面试耽误了几天,今天刷了07,09都是字符串处理,一个是大数反转,一个是回文数判断,我都是转成字符串处理的,过了是过了,但是挺慢的,先记着,等有机会优化下 题目 给定一个 32 位有符号整数, ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- 2021.07.09 K-D树
2021.07.09 K-D树 前置知识 1.二叉搜索树 2.总是很长的替罪羊树 K-D树 建树 K-D树具有二叉搜索树的形态,对于每一个分类标准,小于标准的节点在父节点左边,大于标准的节点在父节点右 ...
- 2018.07.09 洛谷P2365 任务安排(线性dp)
P2365 任务安排 题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间 ...
- 2018.07.09 顺序对齐(线性dp)
顺序对齐 题目描述 考虑两个字符串右对齐的最佳解法.例如,有一个右对齐方案中字符串是 AADDEFGGHC 和 ADCDEGH. AAD~DEFGGHC ADCDE~~GH~ 每一个数值匹配的位置值 ...
随机推荐
- 原生方法scrollTo支持滚动特效
scrollTo默认的是瞬间滚动到坐标位置, 使用配置方法, behavior属性设置为smooth就可以支持平滑滚动了,不过这种方法兼容性不行,并且无法支持配置滚动速率 // 默认滚动效果,瞬时滚动 ...
- 转:Android Context的理解
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40481055,本文出自:[张鸿洋的博客] 本文大多数内容翻译自:http://w ...
- clips 前端 js 单选按钮与输入框 的配合变化
情形1: 一对单选按钮 一个输入框组 输入框组随单选按钮的改变而替换文字或执行其它 片段属于 介绍单选框的基本使用方式 : 1.单选框是有分类的,每个单选框有自己所属的组 从而一个页面可以拥有多组单 ...
- 64位操作系统安装32位客户端和PL/SQL
PL/SQ只能使用32位的Oracle客户端.在64位系统下安装了64位的oracle 11g,使用PL/SQL需再安装32位Oracle客户端. 按以下方法试验成功: 1)安装32位的Oracle客 ...
- DNS 资源记录解释
;SOA授权的开始;;SOA或授权的开始记录用来表示区域的启动;每个区域必须只有一个SOA记录;从名字服务器,在不能和主服务器通信的情况下,将提供12小时DNS服务, 在指定的时间后停止为那个区域提供 ...
- Linux应用层的定时器Timer使用详解【转】
转自:http://blog.csdn.net/wwwtovvv/article/details/8601528 版权声明:本文为博主原创文章,未经博主允许不得转载. linux下定时器的使用 -- ...
- TCP/IP、Http、Socket 简单理解
转自:http://blog.csdn.net/guyan0319 https://blog.csdn.net/guyan0319/article/details/79404216 一. 什么是TCP ...
- delphi 开机自动运行代码
unit Unit1;//download by http://www.codefans.netinterface uses Windows,Registry, Messages, SysUtils ...
- Reporting Services 的一些问题
做报表有一段时间,感觉Reporting Services还是比较麻烦的,因为网上的例子有的比较少,很多地方都需要自己摸索,说说我遇到的一些问题 1:页首的设计 一些固定显示的资料放在页首里面比较方便 ...
- Python的程序结构[1] -> 方法/Method[0] -> 类实例方法、私有方法和抽象方法
类实例方法.私有方法和抽象方法 Python中最常用的就是类实例方法,类似于属性中的类实例属性,同时,也存在与私有属性类似方法,即私有方法,下面介绍这两种常见的方法,以及一种特殊意义的类实例方法 -- ...