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 ...
 
随机推荐
- 转: oracle中schema指的是什么?
			
看来有的人还是对schema的真正含义不太理解,现在我再次整理了一下,希望对大家有所帮助. 我们先来看一下他们的定义:A schema is a collection of database obje ...
 - 17 - 路径操作-shutil模块
			
目录 1 路径操作 1.1 os.path模块 1.2 pathlib模块 1.2.1 目录操作 1.2.2 文件操作 1.3 os 模块 2 shutil模块 2.1 copy复制 2.2 rm删除 ...
 - 服务号使用微信网页授权(H5应用等)
			
获取授权准备 AppId 服务号已经认证且获取到响应接口权限 设置网页授权域名 公众号设置 - 功能设置 - 网页授权域名.注意事项: 回调页面域名或路径需使用字母.数字及"-"的 ...
 - oracle只要第一条数据SQL
			
select * from ( select * from COMMON_BIZREL_WF where sponsor is not null order by serialid ) where r ...
 - sed的额外用法(网摘)
			
#在我开始动手写一个一个的脚本的时候才会看到更多的用法 1. 在某行的前一行或后一行添加内容(前提是要确定行的内容) # 匹配行前加 sed -i '/allow/ideny' httpd.conf ...
 - java基础2 判断语句:if ... else 语句和 switch 语句
			
一.if ... else 判断语句 1.if ... else 判断语句的格式 1.1.格式一 if(判断条件){ 执行不满足条件的语句 } 1.2.格式二 if(判断语句){ 满足条件的语句 }e ...
 - web 端 gantt组件选型
			
gantt - 甘特图 甘特图(Gantt chart)又称为横道图.条状图(Bar chart).其通过条状图来显示项目,进度,和其他时间相关的系统进展的内在关系随着时间进展的情况.以提出者亨利·L ...
 - scala学习6--collection
			
list的下标访问 var t = List(1,2,3,5,5) println(t(2)) map函数 println(t.map(a=> {print("***"+a ...
 - 【LOJ】 #2011. 「SCOI2015」情报传递
			
题解 一写过一交A的一道数据结构水题 我们发现大于C可以转化为这条路径上有多少个在某天之前开始调查的情报员,离线全部读入,变成树上路径查询某个区间的数出现过多少次,构建一棵根缀的主席树,查询的时候用两 ...
 - linux命令---查找文件中的内容
			
linux命令---查找文件中的内容 [yang@localhost ~]$ cat 1.txt |egrep '123456789|second'-------匹配123456789或者seco ...