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. [005] unique_sub_string

    [Description] Given a string, find the largest unique substring. e.g. str[] = "asdfghjkkjhgf&qu ...

  2. Petrozavodsk Summer Training Camp 2017 Day 9

    Petrozavodsk Summer Training Camp 2017 Day 9 Problem A. Building 题目描述:给出一棵树,在树上取出一条简单路径,使得该路径的最长上升子序 ...

  3. ie7浏览器兼容问题

    win10 下如何调试Ie 网上有很多ie的测试工具,包括ms自己出的有,但是如果是win10系统,压根不需要这些玩意. win10 浏览器edge虽然是重写过的,但是win10并没有完全抛弃ie,可 ...

  4. acm专题---最小生成树

    kruscal(eloge): 题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N ...

  5. csu 1801(合数分解+排列组合)

    1801: Mr. S’s Romance Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 15  Solved: 5[Submit][Status][W ...

  6. 完美解决doc、docx格式word转换为Html

    http://blog.csdn.net/renzhehongyi/article/details/48767597

  7. anaconda不错的

  8. karma+seajs

    下面的介绍以karma能正常运行为前提,看karma系列文章:http://www.cnblogs.com/laixiangran/tag/Karma/ 目录结构 步骤 安装 npm install ...

  9. Scrapy 笔记(三)

    摘抄自Python 一.随机user-agent 的设置 关于配置和代码 这里我找了一个之前写好的爬虫,然后实现随机更换User-Agent,在settings配置文件如下: DOWNLOADER_M ...

  10. Dfs【P2052】 [NOI2011]道路修建

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...