这个题据说是动归、、、不过我没学过啊,反正最后给AC了。。。。无所谓了
方法效率可能差点,下面是我的代码
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define MAX_SIZE 100000 + 5
int main(){
int n;
int array[MAX_SIZE];
scanf("%d",&n);
memset(array,0,sizeof(array));
for(int i=1;i<=n;i++){
int t;
scanf("%d",&t);
array[i]=t;
}
/*array[t]=i´ú±ítÅŵÚi*/
int arr[MAX_SIZE];
int t;
while(scanf("%d",&t)!=EOF){
arr[t]=1;
for(int i=2;i<=n;i++){
scanf("%d",&t);
arr[t]=i;
}
int max_size = 1;
for(int i=1;i<=n;i++){
if(n+1-i<=max_size) break;
int counts[MAX_SIZE]={0};
int temp[MAX_SIZE];
int top=1;
counts[0]=1;
temp[0]=arr[i];
for(int j=i+1;j<=n;j++){
for(int k=0;k<top;k++){
max_size=max_size>counts[k]?max_size:counts[k];
if(array[temp[k]]<array[arr[j]]){
counts[top]=counts[k]+1;
temp [top]=arr[j];
top++;
}
}
}
}
printf("%d\n",max_size);
}
return 0;
}
下面是网上的代码
#include <stdio.h>
#include <string.h>
#define MAX 100
#define max(a,b) (a) > (b) ? (a) : (b)
int ans,dp[MAX][MAX];
int n,arr[MAX],brr[MAX];
int main()
{
int i,j,k;
scanf("%d",&n);
for (i = 1; i <= n; ++i)
scanf("%d",&k),arr[k] = i;
while (scanf("%d",&k) != EOF) {
ans = 0,brr[k] = 1;
for (i = 2; i <= n; ++i)
scanf("%d",&k),brr[k] = i;
memset(dp,-1,sizeof(dp));
for (i = 0; i <= n; ++i)
dp[0][i] = dp[i][0] = 0;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j) {
if (arr[i] == brr[j])
dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
ans = max(dp[i][j],ans);
}
printf("%d\n",ans);
}
}