UVa111History Grading

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

Given the correct chronological order of n events tex2html_wrap_inline34 as tex2html_wrap_inline36 where tex2html_wrap_inline38 denotes the ranking of event i in the correct chronological order and a sequence of student responses tex2html_wrap_inline42 wheretex2html_wrap_inline44 denotes the chronological rank given by the student to event i;


#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;
}


原文地址:https://www.cnblogs.com/lgh1992314/p/5835155.html