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 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问题)的更多相关文章

  1. uva 111 - History Grading (dp, LCS)

    题目链接 题意:给N,第二行是答案,n个数c1---cn, 代表第一个的顺序是c1,第二个数顺序是c2; 下面每一行是学生的答案,格式同上. 注意:这个给的顺序需要处理一下,不能直接用. 思路:LCS ...

  2. UVa 111 History Grading (简单DP,LIS或LCS)

    题意:题意就是坑,看不大懂么,结果就做不对,如果看懂了就so easy了,给定n个事件,注意的是, 它给的是第i个事件发生在第多少位,并不是像我们想的,第i位是哪个事件,举个例子吧,4 2 3 1, ...

  3. uva 111 History Grading(lcs)

    题目描述 在信息科学中有一些是关于在某些条件限制下,找出一些计算的最大值. 以历史考试来说好了,学生被要求对一些历史事件根据其发生的年代顺序来排列.所有事件顺序都正确的学生无疑的可以得满分.但是那些没 ...

  4. uva 111 History Grading(最长公共子序列)

    题目连接:111 - History Grading 题目大意:给出一个n 代表序列中元素的个数, 然后是一个答案, 接下来是若干个同学的答案(直到文件结束为止), 求出两个序列的最长公共子序列, 注 ...

  5. uva 10635 LCS转LIS

    这道题两个数组都没有重复的数字,用lcs的nlogn再适合不过了 #include <iostream> #include <string> #include <cstr ...

  6. UVa 10723 LCS变形 Cyborg Genes

    题解转自: UVA 10723 Cyborg Genes - Staginner - 博客园 首先这个题目肯定是按最长公共子序列的形式进行dp的,因为只有保证消去的一部分是最长公共子序列才能保证最后生 ...

  7. UVA 10534 LCS变种题

    求一个序列中 的2*n-1个数字 ,前n+1个数字为严格升序 后n+1个为严格降序,求最长的长度 一开始还没想清楚怎么解,其实就是一个LCS问题,从头到尾以及反序求一下LCS 由于 d[i]为包含了自 ...

  8. UVA 111 History Grading

    读题读了好久,其实就是在输入数据时要对数据的位置进行相应的改变 #include<iostream> #include<cstring> #include<cstdio& ...

  9. UVa 111 - History Grading (by 最长公共子序列 )

     History Grading  Background Many problems in Computer Science involve maximizing some measure accor ...

随机推荐

  1. spring mvc 注解详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  2. [uva11997]k个最小和

    一个k*k的矩阵,每行选取一个数相加则得到一个和,求最小的前k个和. k<=750 已知前m行最小的前k个和d[1]…d[k],则前m+1行最小的前k个和都必定是d[i](i<=k)+a[ ...

  3. HDU5852 Intersection is not allowed!

    There are K pieces on the chessboard. The size of the chessboard is N*N. The pieces are initially pl ...

  4. vs调试 配置IISExpress允许局域网内部访问

    内网可访问后,本机不能使用localhost   1.找到IISExpress的配置文件,位于 <文档>/IISExpress/config文件夹下,打开applicationhost.c ...

  5. perl发送post数据

    把post数据写进一个匿名数组里就行 #!/usr/bin/env perl -w use strict; use LWP::UserAgent; my $ua = LWP::UserAgent-&g ...

  6. java===java基础学习(15)---抽象,接口

    抽象 //这就是一个抽象类 abstract class Animal { String name; int age; abstract public void cry(); } //当一个类继承的父 ...

  7. 爬虫===登陆CSDN的方法

    本文主要介绍csdn的登陆,可应用在爬虫上~ # -*- coding:utf-8 -*- import json import requestsfrom xlutils.copy import co ...

  8. sicily 1172. Queens, Knights and Pawns

    Description You all are familiar with the famous 8-queens problem which asks you to place 8 queens o ...

  9. C中级 MariaDB Connector/C API 编程教程

    引言 - 环境搭建 首先开始环境搭建. 主要在Window 10 + Visual Studio 2015 上构建使用 mariadb connector/c api 进行数据操作开发. 为什么选择在 ...

  10. C基础 如何得到文件长度

    引言 有一天看见看到返回文件长度代码返回值都是long,就感觉怪怪的, 一般32位long最大也就2G. 而大文件太多了, 一个Dota2安装包估计都得10多G吧. 一般C得到文件长度代码 /* * ...