您的位置:首页 > 其它

1045. Favorite Color Stripe (30)

2017-02-14 16:17 393 查看
动态规划题,一步一步加入最爱的序列的元素

#include<iostream>
#include<vector>

using namespace std;
int maxlength;
struct node {
int data;
int max;//存储当前至此点的最大序列长度
node() { max = 0; }
};
vector<int> fav;//存储最爱的序列
vector<node> resault;
int N;
int main()
{
cin >> N;
int n,m;
cin >> n;
fav.resize(n);
for (int t = 0;t < n;t++)
cin >> fav[t];
cin >> m;
resault.resize(m);

for (int t = 0;t < m;t++)
cin >> resault[t].data;//最爱序列赋值
for (auto x : fav) {
for (auto it = resault.begin();it != resault.end();it++)
{
if (it == resault.begin())//特殊处理最开始的点
{
if (it->data == x)it->max = 1;
}
else {
if (it->data == x) it->max = (it - 1)->max + 1;//当遇到与x相同的位置,+1
else it->max=(it - 1)->max >it->max ? (it - 1)->max  : it->max;//比较不加入x时当前位置的最大的序列长度和加入x后前一位置的最大序列长度,去大者
}
}
}
cout << resault[m - 1].max << endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT-甲 动态规划