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 would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.
It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.
Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (≤200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤104) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.
Output Specification:
For each test case, simply print in a line the maximum length of Eva's favorite stripe.
Sample Input:
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
7
思路:设dp[i][j]为考虑从第0到i种颜色,考虑到长度从0到第j时的最长结果,那么状态转移方程是显而易见的。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int n,m,l;
int color[205];
int a[10004];
int dp[205][10005];
int main()
{
cin>>n;
cin>>m;
for(int i=0;i<m;i++)cin>>color[i];
cin>>l;
for(int i=0;i<l;i++)cin>>a[i];
dp[0][0]=(color[0]==a[0])?1:0;
for(int j=1;j<l;j++)dp[0][j]=(color[0]==a[j])?dp[0][j-1]+1:dp[0][j-1];
for(int i=1;i<m;i++)dp[i][0]=(color[i]==a[0])?1:dp[i-1][0];
for(int i=1;i<m;i++)
{
for(int j=1;j<l;j++)
{
if(color[i]==a[j])dp[i][j]=max(dp[i-1][j],dp[i][j-1]+1);
else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout<<dp[m-1][l-1]<<endl;
return 0;
}
pat 甲级 1045 ( Favorite Color Stripe ) (动态规划 )的更多相关文章
- PAT甲级1045. Favorite Color Stripe
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
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$的子序列 (这个子序列的相邻元素可以重复) ...
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- 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 (30分)(简单dp)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- 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 ...
随机推荐
- # localhost 、217.0.0.1 、本机IP
localhost .217.0.0.1 .本机IP localhost是一个域名,性质跟 "www.baidu.com" 差不多,指向127.0.0.1这个IP地址,在windo ...
- Neo4j
Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络(从数学角度叫做 ...
- mysql数据库的锁表与解决办法(原博客url:http://www.cnblogs.com/wanghuaijun/p/5949934.html)
MySQL锁概述 相对其他数据库而言,MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制.比如,MyISAM和MEMORY存储引擎采用的是表级锁(table-level loc ...
- dos(cmd)命令大全(本文摘至于网络)
命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在 ...
- Python字符串常用的方法——真心觉得比java,c好用
# Strings have many methods wo can use rand_string=" life is a beautiful struggle " print( ...
- Unity Button延迟功能
有时候Button点下去不是要求立即反应的,而是先有个特别短的动画,再反应. 实现: 继承Button,然后重写一下OnPointerClick,利用协程来延迟. using System.Colle ...
- 华为机试题:仿LISP
package com.nowcoder.huawei; import java.util.*; public class LISP { // 只通过80% // (+ (* 2 3) (^ 4)) ...
- JAVA8新特性随笔
Instant:瞬时实例 LocalDate:本地日期,不包含具体时间.例如:2014-01-14可以用来记录生日.纪念日.加盟日等. LocalTime:本地时间,不包含日期 LocalDateTi ...
- vue项目在IE浏览器和360兼容模式下页面不显示问题,亲测有效
解决方法:安装 "babel-polyfill" 1.命令:cnpm install --save-dev babel-polyfill 2.在入口main.js文件引入:impo ...
- 利用django 实现个人博客 全记录(二)
上一篇文章已经把基础环境搭建好了 一 创建app D:\学习\python3.7.3\python manage.py startapp blog 修改 博客的 models.py ) ) def ...