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. 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest

    2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...

  2. html的loadrunner脚本

    Action(){ char strs[20]; lr_start_transaction("api_sync_order");   web_add_header("SO ...

  3. rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library

    使用yum安装出现问题:rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library 解决 ...

  4. python基础(12)--初识Socket

    socket: 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思. ...

  5. Hilite代码高亮工具

    在用<有道云笔记>等软件时候,软件自身不提供代码高亮功能,对于需要记录code的学习笔记,视觉效果丢失. 有很多在线工具能用来代码高亮,比如oschina就有代码高亮页面用于着色. 但是我 ...

  6. 【Mac】【已解决】连接Android机器提示“此电脑不能读取您插入的磁盘”

    出现的报错提示页面截图如下: 解决方案: 下载“Android File Transfer.dmg”安装在Mac. 打开USB调试,连接手机即可读取手机磁盘.   下载链接:https://www.t ...

  7. sed实践

    后来也找到一篇文章讲的很详细: http://www.cnblogs.com/ctaixw/p/5860221.html --------------------------------------- ...

  8. vue js moment.js 过滤了双休日和法定节假日

    源码:注!原创的!!!! <template> <div id="DATE"> <ul class="dateForm" @cha ...

  9. Typo: In word 拼写检查

    Settings->Inspections > Spelling > Typo 评写检查,

  10. Gitlab基本管理<一>

    一. 创建Gitlab中第一个项目 1. Gitlab项目的可见类型有三种级别. Private project: 该级别是只有项目拥有者或者已经得到授权的人可以访问该项目,或者这些人是该项目组的成员 ...