【DP】UVA 111 History Grading
输入的序列是表示 ai的位置上值为 i
然后就是最长公共子序列
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define IN freopen ("in.txt" , "r" , stdin);
#define OUT freopen ("out.txt" , "w" , stdout);
typedef long long LL;
const int MAXN = 2111;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=1000000007;
int a[44],b[44],dp[44][44];
int main()
{
int n,x;
//IN;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
a[x]=i;
}
while(~scanf("%d",&x))
{
b[x]=1;
memset(dp,0,sizeof(dp));
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
b[x]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i]==b[j])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
printf("%d\n",dp[n][n]);
}
return 0;
}

浙公网安备 33010602011771号