UVa111History Grading

求最长公共 子序列,只是必须先对输入数据执行cin >>x; str[x] = i;的操作。

Given the correct chronological order ofneventstex2html_wrap_inline34astex2html_wrap_inline36wheretex2html_wrap_inline38denotes the ranking of eventiin the correct chronological order and a sequence of student responsestex2html_wrap_inline42wheretex2html_wrap_inline44denotes the chronological rank given by the student to eventi;


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 25
int n;
int dp[MAX][MAX];
int maps[MAX][MAX];
int str[MAX];
int dest[MAX];

int LCS()
{
	int i, j;
	for(i = 1; i <= n; i++)
	{
		for(j = 1; j <= n; j++)
		{
			if(str[i] == dest[j])
			{
				dp[i][j] = dp[i-1][j-1] + 1;
			}
			else
			{
				dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
			}
		}
	}
	return dp[n][n];
}

int main()
{
	freopen("in.txt", "r", stdin);
	int i, j, x;
	cin >> n;
	for(i = 1; i <= n; i++)
	{
		cin >> x;
		str[x] = i;
	}
	for(i = 0; i <= n; i++)
	{
		dp[0][i] = dp[i][0] = 0;
	}
	while(cin >> x)
	{
		dest[x] = 1;
		for(j = 2; j <= n; j++)
		{
			cin >> x;
			dest[x] = j;
		}
		cout << LCS() << endl;
		memset(dp, 0, sizeof(dp));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值