求最长公共 子序列,只是必须先对输入数据执行cin >>x; str[x] = i;的操作。
Given the correct chronological order ofneventsaswheredenotes the ranking of eventiin the correct chronological order and a sequence of student responseswheredenotes 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;
}