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; }
相关文章推荐
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- PAT 1045. Favorite Color Stripe (30) 变种LCS或LIS
- PAT 1045. Favorite Color Stripe (30)
- PAT 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)-PAT甲级真题
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30) -LCS同意元素反复
- 【PAT】1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- PAT 1045. Favorite Color Stripe (30)(按一定顺序找出最长的子序列,动态规划)
- 1045. Favorite Color Stripe (30)
- 1045. Favorite Color Stripe (30)
- PAT 1045. Favorite Color Stripe (30) 贪心,有序序列问题
- 1045. Favorite Color Stripe (30)-PAT甲级真题
- 1045. Favorite Color Stripe (30) 简单动态规划(LCS的变形)
- PAT_A 1045. Favorite Color Stripe (30)