uva 111 History Grading
简单的动态规划题,不过题目意思很蛋疼;
刚刚开始认为是最长上升子序列,错的一塌糊涂;
后来看了斌牛的题解才知道是最长公共子序列;
题解:
当a[i]==b[j],d(i,j)=(i-1,j-1)+1;
else d(i,j)=max(d(i,j-1),d(i-1,j));
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #define maxn 25 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int a[maxn],b[maxn],f[maxn][maxn]; 7 int main() 8 { 9 int n,x; 10 scanf("%d",&n); 11 for(int i=1; i<=n; i++) 12 { 13 scanf("%d",&x); 14 a[x]=i; 15 } 16 while(scanf("%d",&x)!=EOF) 17 { 18 b[x]=1; 19 for(int i=2; i<=n; i++) 20 { 21 scanf("%d",&x); 22 b[x]=i; 23 } 24 memset(f,0,sizeof f); 25 for(int i=1;i<=n;i++) 26 for(int j=1;j<=n;j++) 27 { 28 if(a[i]==b[j])f[i][j]=f[i-1][j-1]+1; 29 else f[i][j]=max(f[i-1][j],f[i][j-1]); 30 } 31 printf("%d\n", f[n][n]); 32 } 33 return 0; 34 }