循环判断,当a[i] > b[j]时,用一个maxx记录此时的最大公共上升子序列长度,当a[i] = b[j]时,此时的最大公共上升子序列就是maxx+1
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a[505], b[505], f[505];
int main()
{
int t; scanf("%d",&t);
int n, m;
while(t--){
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
for(int i = 1; i <= m; i++){
scanf("%d",&b[i]);
}
int ans = 0, maxx;
memset(f,0,sizeof(f));
for(int i = 1; i <= n; i++){
maxx = 0;
for(int j = 1; j <= m; j++){
if(b[j] < a[i] && maxx < f[j]) maxx = f[j];
else if(a[i] == b[j]){
f[j] = maxx + 1;
ans = max(ans,f[j]);
}
}
}
printf("%d\n",ans);
if(t) printf("\n");
}
return 0;
}