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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. PAT 甲级 1045 Favorite Color Stripe

    https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...

  4. PAT 甲级 1045 Favorite Color Stripe(DP)

    题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...

  5. 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 ...

  6. 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. ...

  7. 1045 Favorite Color Stripe 动态规划

    1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. perl6正则 2: 字母,数字,空格,下划线, 字符集

    数字, 字母, 下划线 在perl6中, 如果是 数字, 字母, 下划线, 在正则里可以正接写上. > so / True > so 'perl6_' ~~ /_/ True > 非 ...

  2. 【SSH项目实战】脚本密钥的批量分发与执行【转】

    [TOC] 前言 <项目实战>系列为<linux实战教学笔记>第二阶段内容的同步教学配套实战练习,每个项目循序衔接最终将组成<Linux实战教学笔记>第二阶段核心教 ...

  3. linux系统定时任务设置

    .使用at命令设置一次性定时任务 2.使用crontab设置周期性定时任务 1)cd /home 目录下,使用vi test.py创建文件,内容如下: #!/usr/bin/python#coding ...

  4. java中参数传递--值传递,引用传递

    java中的参数传递——值传递.引用传递   参数是按值而不是按引用传递的说明 Java 应用程序有且仅有的一种参数传递机制,即按值传递. 在 Java 应用程序中永远不会传递对象,而只传递对象引用. ...

  5. struct中长度为0的数组用途与原理

    前言 在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNUC中,存在一个非常奇怪的用法,那就是长度为0的数组,比如Array[0]; 很多人可能觉得不可思议,长度为0的数组是没有什么意义的, ...

  6. LightOJ - 1297 Largest Box LightOJ(一元三次方程求极大值)

    题目链接:https://vjudge.net/contest/28079#problem/K 题目大意:给你一个长为L,宽为W的纸片,四个角剪掉边长为x的正方形,如下图所示,然后折成一个无盖的纸盒, ...

  7. (三)Spring 之AOP 详解

    第一节:AOP 简介 AOP 简介:百度百科: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是Spring框架中的一个 ...

  8. K8S网络排故障一则--iptables规则

    这个故障源起来在k8s上同时安装ceph群集(测试的时候机器不多啊) 当这两者都OK之后,原来k8s上的服务实例,则有的通,有的不通了. ==================== 所有可能的故障点,f ...

  9. mysql中行转列与列传行的问题

    行转列: 使用cross join 的方式 使用case-when的方式 列转行: SELECT user_name, REPLACE ( substring_index(mobile, ',', a ...

  10. AciveX接口事件的手动实现

    为了理解ActiveX事件的运作原理,特意做了如下实验 初试化过程: try { CLSID clsid; HRESULT hr=::CLSIDFromProgID(L"MSWinsock. ...