PAT (Advanced Level) 1045 Favorite Color Stripe (DP)

xiaoxiao2021-03-01  46

 dp[i][j]表示fav中第i个数匹配到stripe第j个数时,所能获得的最大长度。 

由于同样的数字可以重复,因此状态转移方程:

#include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<algorithm> using namespace std; int n, m, l; int fav[205]; int stripe[10005]; int dp[205][10005]; int main(){ scanf("%d%d",&n,&m); for(int i = 1; i <= m; ++i) scanf("%d",&fav[i]); scanf("%d",&l); for(int i = 1; i <= l; ++i) scanf("%d",&stripe[i]); memset(dp,0,sizeof(dp)); for(int i = 1; i <= m; ++i){ for(int j = 1; j <= l; ++j){ int maxn = max(dp[i][j-1],dp[i-1][j]); if(stripe[j] == fav[i]) dp[i][j] = maxn + 1; else dp[i][j] = maxn; } } // for(int i = 1; i <= m; ++i){ // for(int j = 1; j <= l; ++j){ // printf("%d ",dp[i][j]); // } // cout <<endl; // } printf("%d",dp[m][l]); return 0; }

 

转载请注明原文地址: https://www.6miu.com/read-4149955.html

最新回复(0)