[pat]1045. Favorite Color Stripe (30)

https://www.patest.cn/contests/pat-a-practise/1045

动态规划。算法思路借鉴:http://www.cnblogs.com/jackwang822/p/4749085.html

#pragma warning(disable:4996)
#include<vector>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
vector<int> vfc;
vector<int> stripe;

int main(){
	int n,m,l;
	cin>>n>>m;
	vector<int> ncolor(n+1, 0);
	for(int i = 0;i < m; i++){
		int a;
		cin>>a;
		vfc.push_back(a);
	}
	cin>>l;
	for(int i = 0;i < l; i++){
		int a;
		cin>>a;
		stripe.push_back(a);
	}
	vector<vector<int>> dp;
	vector<int> a(stripe.size()+1, 0);
	dp.push_back(a);
	for(int i = 0; i < vfc.size(); i++){
		vector<int> b(stripe.size()+1, 0);
		dp.push_back(b);
		for(int j = 0; j < stripe.size(); j++){
			int max = dp[i+1][j] > dp[i][j+1] ? dp[i+1][j] : dp[i][j+1];
			if(stripe[j] == vfc[i]){
				dp[i+1][j+1] = max + 1;
			}
			else{
				dp[i+1][j+1] = max;
			}
		}
	}
	cout<<dp[dp.size()-1][dp[0].size()-1]<<endl;
	system("pause");
	return 0;
}


你可能感兴趣的:(C++,动态规划,pat,OJ)