lcs模板题,看懂题目应该没有问题了。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 21
int std_order[MAXN], submit_order[MAXN], dp[MAXN][MAXN];
int lcs(const int &n)
{
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
if( std_order[i] == submit_order[j] ) {
dp[i][j] = dp[i-1][j-1]+1; continue;
}
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[n][n];
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, v;
scanf("%d", &n);
memset(std_order, 0, sizeof(std_order));
for(int i = 1; i <= n; i ++) {
scanf("%d", &v); std_order[v] = i;
}
while( ~scanf("%d", &v) ) {
submit_order[v] = 1;
for(int i = 2; i <= n; i ++) {
scanf("%d", &v); submit_order[v] = i;
}
printf("%d\n", lcs(n));
}
return 0;
}
uva_111
最新推荐文章于 2019-10-31 23:45:00 发布