uva:111 - History Grading

12 篇文章 0 订阅

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=47

LCS问题:动规方程:dp [i][j] = dp[i-1][j-1] +1 (numx[i] = numy[j]) || max{ dp[i-1] [j] , dp[i][j-1] } || 0 (i=0 || j =0) ;

注意此题题目的意思,有点绕人,看数据的时候感觉很奇怪,然后再仔细看题,才知道,输入的一系列数,对应位i上的数a[i]表示的是第i件事情发生的时间。

代码如下:

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

using namespace std ;

const int maxn = 25 ;

int n;
int numx[maxn] ;
int numy[maxn] ;
int dp[maxn][maxn] ;

int main()
{
	//freopen("data.in" , "r" , stdin) ;

	int i ;
	int j ;
	int x ;

	cin>>n;

	for(i = 1 ; i <= n ; i ++)
	{
		cin>>x ;
		numx[x] = i ;
	}

	while(cin>>x)
	{
		numy[x] = 1 ;

		for(j = 2 ; j <= n ; j ++)
		{
			cin>>x ;
			numy[x] = j ;
		}

		memset(dp , 0 , sizeof(dp)) ;

		for(i = 1 ; i <= n ; i ++)
		{
			for(j = 1 ; j <= n ; j ++)
			{
				if(numx[i]==numy[j])
					dp[i][j] = dp[i-1][j-1] + 1 ;
				else
					dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1] ;
			}
		}

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

	return 0 ;
}
我刚刚有重新用记忆化搜索的方式交了一下这个题目,需要注意的是:在初始化的时候应该初始化为-1,否则会出错,代码如下:
#include<iostream>
#include<string.h>
#include<stdio.h>

using namespace std ;

const int maxn = 1005 ;

char numa[maxn] ;
char numb[maxn] ;
int  num[maxn][maxn] ;

int lena ;
int lenb ;

int dp(int , int ) ; 
int main()
{
	//freopen("data.in" , "r" , stdin) ;

	while(1)
	{
		cin.getline(numa , sizeof(numa)) ;
		if(!cin)
			break ;
		cin.getline(numb  , sizeof(numb)) ;
		
		lena = strlen(numa ) ;
		lenb = strlen(numb ) ;

		memset(num , -1 , sizeof(num)) ;

		dp(lena - 1 , lenb - 1) ;

		printf("%d\n" , num[lena-1][lenb-1]) ;
	}
	return 0 ;
}

int dp(int n , int m)
{
	int & ans = num[n][m] ;
	
	if(n < 0 || m < 0)
		return 0 ;

	if(ans >= 0)
	{
		return ans ;
	}

	if(numa[n]==numb[m])
	{
		ans = dp(n - 1 , m - 1) + 1 ;
	}

	else if(dp(n-1 , m) >= dp(n , m - 1))
	{
		ans = dp(n-1 , m) ;
	}
	else ans = dp(n , m - 1) ;

	return ans ;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
补全以下代码private String cid;// Course id, e.g., CS110. private String name;// Course name, e.g., Introduce to Java Programming. private Integer credit;// Credit of this course private GradingSchema gradingSchema; //Grading schema of this course // enum GradingSchema{FIVE_LEVEL, PASS_FAIL} private Integer capacity;// Course capacity. private Integer leftCapacity;// Course capacity left. You should update the left capacity when enrolling students. private Set<Timeslot> timeslots;// One course may have one or more timeslots. e.g., a lecture in Monday's 10:20-12:10, and a lab in Tuesday's 14:00-15:50. public Course(String cid, String name, Integer credit, GradingSchema gradingSchema, Integer capacity) // constructor public void addTimeslot(Timeslot timeslot) //Record a timeslot for this course private Integer id;// A unique student id, should be an 8-digit integer: Undergraduates' ids should start with 1; Postgraduates' ids should start with 3. e.g., 12213199. private String name;// Student’s name private Map<Course, Grade> courses;// Enrolled courses, using Map structure to store course and its grade as a pair. Grade is an enum type enum Grade{PASS,FAIL,A,B,C,D,F}with an attribute: Double gradePoint protected Student(Integer id, String name) // constructor public abstract boolean canGraduate() // Checks if this student satisfies all the graduating conditions. Hint: you are allowed to change this abstract method into non-abstract to check if the student satisfies the common graduation conditions. public void enroll(Course course) // Tries to enroll the course, do some checks before enrolling. public void recordGrade(Course course, Grade grade)// Records the grade of a course that is current learning. public double getGpa() // Calculates the GPA for this student. public UndergraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for undergraduate students public PostgraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for postgraduate students
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值