UVA 111(LCS问题)
| 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 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 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 n integers, indicating the correct chronological order of n events. The remaining lines will each consist of nintegers 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 思路:化简成LCS问题;
正确排序s【】与学生排序p【】的LCS问题:
定义状态转移方程:f【i】【j】为p【】的前i个元素与s【】的前j个元素的LCS长度;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
f[i][j]=max(f[i][j-1],f[i-1][j]);
if(s[j]==p[i])
f[i][j]=max(f[i][j],f[i-1][j-1]+1);
}
边界为f[0][0]=0;
注意:输入时s[]与p[]的后缀是事件时间;内容是位置;如果调换了会WA;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define PI 3.141592653589792128462643383279502
int p[],s[],n;
int f[][];
int main(){
//#ifdef CDZSC_June
freopen("in.txt","r",stdin);
//#endif
//std::ios::sync_with_stdio(false);
cin>>n;
int x;
memset(p,,sizeof(p));
memset(s,,sizeof(s));
for(int i=;i<=n;i++){
cin>>x;s[x]=i;
}
while(!cin.eof()){
for(int i=;i<=n;i++){
cin>>x;p[x]=i;
}
memset(f,,sizeof(f));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
f[i][j]=max(f[i-][j],f[i][j-]);
if(s[i]==p[j])
f[i][j]=max(f[i][j],f[i-][j-]+);
}
cout<<f[n][n]<<endl;
}
return ;
}
UVA 111(LCS问题)的更多相关文章
- uva 111 - History Grading (dp, LCS)
题目链接 题意:给N,第二行是答案,n个数c1---cn, 代表第一个的顺序是c1,第二个数顺序是c2; 下面每一行是学生的答案,格式同上. 注意:这个给的顺序需要处理一下,不能直接用. 思路:LCS ...
- UVa 111 History Grading (简单DP,LIS或LCS)
题意:题意就是坑,看不大懂么,结果就做不对,如果看懂了就so easy了,给定n个事件,注意的是, 它给的是第i个事件发生在第多少位,并不是像我们想的,第i位是哪个事件,举个例子吧,4 2 3 1, ...
- uva 111 History Grading(lcs)
题目描述 在信息科学中有一些是关于在某些条件限制下,找出一些计算的最大值. 以历史考试来说好了,学生被要求对一些历史事件根据其发生的年代顺序来排列.所有事件顺序都正确的学生无疑的可以得满分.但是那些没 ...
- uva 111 History Grading(最长公共子序列)
题目连接:111 - History Grading 题目大意:给出一个n 代表序列中元素的个数, 然后是一个答案, 接下来是若干个同学的答案(直到文件结束为止), 求出两个序列的最长公共子序列, 注 ...
- uva 10635 LCS转LIS
这道题两个数组都没有重复的数字,用lcs的nlogn再适合不过了 #include <iostream> #include <string> #include <cstr ...
- UVa 10723 LCS变形 Cyborg Genes
题解转自: UVA 10723 Cyborg Genes - Staginner - 博客园 首先这个题目肯定是按最长公共子序列的形式进行dp的,因为只有保证消去的一部分是最长公共子序列才能保证最后生 ...
- UVA 10534 LCS变种题
求一个序列中 的2*n-1个数字 ,前n+1个数字为严格升序 后n+1个为严格降序,求最长的长度 一开始还没想清楚怎么解,其实就是一个LCS问题,从头到尾以及反序求一下LCS 由于 d[i]为包含了自 ...
- UVA 111 History Grading
读题读了好久,其实就是在输入数据时要对数据的位置进行相应的改变 #include<iostream> #include<cstring> #include<cstdio& ...
- UVa 111 - History Grading (by 最长公共子序列 )
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
随机推荐
- elementui raido 单选框 循环渲染加:key
<el-radio-group v-model="adminRole"> <el-radio v-for="item in adminRoles&quo ...
- 对象方法、类方法、原型方法 && 私有属性、公有属性、公有静态属性
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 特征工程(Feature Engineering)
一.什么是特征工程? "Feature engineering is the process of transforming raw data into features that bett ...
- python基础===zmail,收发邮件的模块
项目地址: GitHub:https://github.com/ZYunH/zmail 介绍: https://mp.weixin.qq.com/s?__biz=MzAxMjUyNDQ5OA==&a ...
- c++设计模式系列----builder模式
看了好几处关于builder模式的书和博客,总感觉不是很清楚,感觉不少书上的说的也不是很准确.最后还是看回圣经<设计模式>.看了好久终于感觉明白了一点了. 意图: builder模式提出的 ...
- springboot基础知识学习
一.springboot中常用的注解: 原文链接:http://blog.csdn.net/lafengwnagzi/article/details/53034369 原文链接:http://www. ...
- Struts2学习笔记02 之 使用
一.页面向Action传参 1.基本属性注入,页面命名name,action提供成员变量name并提供set方法. 2.域模型注入:页面用user.name对象点属性形式.action成员user对象 ...
- MySQL的读写分离---主从复制、主主复制
1.复制是基于BinLog日志 存在三种日志格式:Statement:存储Sql语句,存储日志量是最小的.有可能复制不一致Row:存储event数据,存储日志量大,但是不能很直接进行读取:Mixed: ...
- Restore IP Addresses——边界条件判定
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode Binary Tree PostorderTranversal
Problem Description Given a binary tree, return the postorder traversal of its nodes' values. For ex ...