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 ...
随机推荐
- 修改vs17中的cordova模板
因为visual studio 2017创建的默认cordova-ios的版本自动编译带有swift语言的插件会出现异常,cordova-ios升级到4.3.1,并且配置build.json能解决问题 ...
- mac上卸载mysql
在终端输入一下命令 sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MyS ...
- centos7安装lamp
一.准备工作 1. 下载并安装CentOS7.2,配置好网络环境,确保centos能上网,可以获取到yum源. centos7.2的网络配置: vim /etc/sysconfig/network ...
- PHP的instanceof关键字
PHP5的另一个新成员是instdnceof关键字.使用这个关键字可以确定一个对象是类的实例.类的子类,还是实现了某个特定接口,并进行相应的操作.在某些情况下,我们希望确定某个类是否特定的类型,或者是 ...
- Suse Linux下NTP缓慢调整配置,转载至http://www.gpstime.com.cn/
(1)系统内若有使用crontab 进行与时间调整相关的例行性工作排程,应注释掉(命令人工crontab -e修改,删除定时同步任务ntpdate -s ntpserver). (2)修改ntp配置文 ...
- UFLDL 教程学习笔记(六)主成分分析
教程:http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks/ 以及这篇博文,写的很清楚:http://blog. ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- [Spring Data JPA问题]Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException
JPQL如下: @Modifying(clearAutomatically = true) @Query("UPDATE SyncTestFromTKDO SET stuAns = '' w ...
- PhpStorm函数注释的设置
首先,PhpStorm中文件.类.函数等注释的设置在:setting->Editor->FIle and Code Template->Includes下设置即可,其中方法的默认是这 ...
- linux技巧-持续更新
终端下锁屏ctrl + s,解锁 ctrl + q 长时间运行命令,防中断 screen 注意,screen命令里面是不可以滚动屏幕,查看以前记录的 : ctrl+A + [ 终端切割屏幕,类似vi ...