题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120
解题报告:dp,用一个串去更新另一个串,递推方程是:
if(b[i] > a[j])
m = max(m,dp[j]);
else if(b[i] == a[j]) dp[j] = m + 1;
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 1005; 7 int a[maxn],b[maxn],dp[maxn]; 8 9 int main() 10 { 11 int T,n,m; 12 scanf("%d",&T); 13 while(T--) 14 { 15 scanf("%d",&n); 16 for(int i = 1;i <= n;++i) 17 scanf("%d",&a[i]); 18 scanf("%d",&m); 19 for(int i = 1;i <= m;++i) 20 scanf("%d",&b[i]); 21 memset(dp,0,sizeof(dp)); 22 for(int i = 1;i <= m;++i) 23 { 24 int m = 0; 25 for(int j = 1;j <= n;++j) 26 { 27 if(b[i] > a[j]) 28 m = max(m,dp[j]); 29 else if(b[i] == a[j]) dp[j] = m + 1; 30 } 31 } 32 int ans = 0; 33 for(int i = 1;i <= n;++i) 34 ans = max(ans,dp[i]); 35 printf("%d\n",ans); 36 } 37 return 0; 38 }
时间: 2024-10-09 06:53:22