PAT甲级1045. Favorite Color Stripe
PAT甲级1045. Favorite Color Stripe
题意:
伊娃正在试图让自己的颜色条纹从一个给定的。她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保持最喜爱的顺序。
据说正常的人眼可以区分大约少于200种不同的颜色,
所以伊娃最喜欢的颜色是有限的。然而,原始条纹可能很长,而Eva希望拥有最大长度的剩余最喜欢的条纹。所以她需要你的帮助找到她最好的结果。
请注意,解决方案可能不是唯一的,但您只需要告诉她最大长度。例如,
给出一条条纹{2 2 4 1 5 5 6 3 1 1 5 6}。如果Eva最喜欢的颜色是以她最喜欢的顺序作为{2 3 1 5 6}给出,那么她有4个可能的最佳解决方案{2 2 1 1 1 5 6},{2 2 1 5 5 5 6},{2 2 1 5 5 6 6}和{2 2 3 1 1 5 6}。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,
第一行包含正整数N(<= 200),它是涉及的颜色总数(因此颜色从1到N编号)。然后下一行以正整数M(<= 200)开始,其次是以她最喜欢的顺序给出的M Eva最喜欢的颜色数字。
最后,第三行以一个正整数L(<= 10000)开始,它是给定条带的长度,后面是条纹上的L个颜色。一行中的所有数字都以空格分隔。
输出规格:
对于每个测试用例,只需在一行中打印Eva最喜欢的条纹的最大长度。
思路:
就是让eva裁剪一块布,eva能裁剪出多长的一块布,并且布的颜色的order要按eva喜欢的颜色来。相当于最长非降序字串的问题。用dp做。时间复杂度O(n*m),n为布的长度,m为order的长度。
ac代码:
C++
// pat1045.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
using namespace std;
//最长的非降子序列
vector<int> origanal;
int main()
{
int n, m, l, temp; //n. color nums: 1 - n; m. order nums; l. origanal length;
//input
cin >> n;
cin >> m;
vector<int> order(n + 1,0); //类hash表储存order
for (int i = 1; i <= m; i++) //order数据
{
scanf("%d", &temp);
order[temp] = i;
}
cin >> l;
for (int i = 0; i < l; i++) //origanal数据
{
scanf("%d", &temp);
temp = order[temp]; //把所有数字替换成order,按1...m排列
origanal.push_back(temp);
}
//handle problem
//dp思路:用count储存 count[i] 表示从开始时到现在,以第i个数为结尾,最长是多少。
int maxlen = 0;
vector<int> count(m + 1, 0);
for (int i = 0; i < l; i++)
{
if (origanal[i] == 0) continue; //不在order,cut
count[origanal[i]]++;
for (int k = origanal[i] + 1; k <= m; k++)
{
if(count[k] < count[origanal[i]])
count[k]++;
}
}
//output
cout << count[m] << endl;
return 0;
}
PAT甲级1045. Favorite Color Stripe的更多相关文章
- PAT 甲级 1045 Favorite Color Stripe (30 分)(思维dp,最长有序子序列)
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. S ...
- pat 甲级 1045 ( Favorite Color Stripe ) (动态规划 )
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
- PAT甲级——A1045 Favorite Color Stripe
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- PAT 1045 Favorite Color Stripe[dp][难]
1045 Favorite Color Stripe (30)(30 分) Eva is trying to make her own color stripe out of a given one. ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- 1045. Favorite Color Stripe (30) -LCS允许元素重复
题目如下: Eva is trying to make her own color stripe out of a given one. She would like to keep only her ...
- 1045. Favorite Color Stripe (30) -LCS同意元素反复
题目例如以下: Eva is trying to make her own color stripe out of a given one. She would like to keep only h ...
随机推荐
- webgote的例子(3)Sql注入(SearchPOST)
Sql注入(Search/POST) (本章内容):post的方式进行注入 今天来讲一下sql注入的另一个例子(post) 上一个用的是get请求的方法将我们的参数传到服务器进行执行 上图中的标红是需 ...
- 读书笔记 effective c++ Item 3 在任何可能的时候使用 const
Const可以修饰什么? Const 关键字是万能的,在类外部,你可以用它修饰全局的或者命名空间范围内的常量,也可以用它来修饰文件,函数和块作用域的静态常量.在类内部,你可以使用它来声明静态或者非 ...
- {%csrf_token%}的作用
<form> {%csrf_token%} </form> 在django中我们需要在templates的form中加入{%csrf_token%}这串内容,它的作用是当我们g ...
- git clone的
git clone git@e.coding.net:wudi360/*******.git
- Hive入门学习随笔(一)
Hive入门学习随笔(一) ===什么是Hive? 它可以来保存我们的数据,Hive的数据仓库与传统意义上的数据仓库还有区别. Hive跟传统方式是不一样的,Hive是建立在Hadoop HDFS基础 ...
- 【PAT】1007. 素数对猜想 (20)
1007. 素数对猜想 (20) 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷多对相 ...
- scrapy 学习笔记1
最近一段时间开始研究爬虫,后续陆续更新学习笔记 爬虫,说白了就是获取一个网页的html页面,然后从里面获取你想要的东西,复杂一点的还有: 反爬技术(人家网页不让你爬,爬虫对服务器负载很大) 爬虫框架( ...
- loadrunner场景执行出现:Failed to Initialize. Reason: TimeOut
问题1: Failed to Initialize. Reason: TimeOut LoadRunner的异常原因(Failed to Initialize. Reason: TimeOut) ...
- LoadRunner 函数大全之中文解释
LoadRunner 函数大全之中文解释 // sapgui_table_set_column_selected 模拟用户 // 单击表中的列标题. int sapgui_table_set_colu ...
- Codeforces Round #371 (Div. 1) C - Sonya and Problem Wihtout a Legend
C - Sonya and Problem Wihtout a Legend 思路:感觉没有做过这种套路题完全不会啊.. 把严格单调递增转换成非严格单调递增,所有可能出现的数字就变成了原数组出现过的数 ...