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 (≤) 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 (≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) 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

题意:

  给出一组favorite color的序列,和一组colors stripe的序列,要求在color stripe序列中找出满足favorite color序列的子串,子串中元素可以重复,只要满足子串颜色的序列和喜欢颜色的序列相同即可。

思路:

  这道题应该用DP来解决,dp[i][j] : 表示在喜欢颜色的序列中以i为下标的颜色结尾,在colors stripe中[:j]中满足喜欢颜色[:i]序列的子串长度。状态转移方程: dp[i][j] = max(dp[i-1])[j], dp[i]][j-1]]; dp[i-1][j]表在colors stripe中[:j]相等的情况下,favorite color的下表向后移动一位,可能是子串长度改变。 dp[i][j-1] : 表示在favorite color中[:i]相等的情况下color stripe下标向后移动一位可能使子串长度发生改变。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 int n, m, l;
7 cin >> n >> m;
8 vector<int> colors(n + 1);
9 set<int> found;
10 for (int i = 1; i <= m; ++i) cin >> colors[i];
11 cin >> l;
12 vector<int> stripe(l + 1);
13 for (int i = 1; i <= l; ++i) cin >> stripe[i];
14 vector<vector<int> > dp(n + 1, vector<int>(l + 1, 0));
15 for (int i = 1; i <= n; ++i) {
16 for (int j = 1; j <= l; ++j) {
17 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
18 if (colors[i] == stripe[j]) dp[i][j]++;
19 }
20 }
21 cout << dp[n][l] << endl;
22 return 0;
23 }

1045 Favorite Color Stripe的更多相关文章

  1. 1045 Favorite Color Stripe 动态规划

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

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

  3. PAT甲级1045. Favorite Color Stripe

    PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...

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

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

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

  7. PAT 甲级 1045 Favorite Color Stripe

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

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

  9. 1045 Favorite Color Stripe (30)(30 分)

    Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...

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

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

随机推荐

  1. 学习了解使用docker

    学习了解使用docker docker是项目开发部署相关工具容器,本文通过官网等资料阅读学习,简单介绍一些基本使用操作: docker是什么 2.docker安装使用 连接进入docker容器 doc ...

  2. tcpdump后台抓包并输出给wireshark

    # 先进到/tmp 目录执行,方便Filezila 传输 # 开启抓包 nohup tcpdump -i eth0 -s0 -nnA 'port 22' -w dump22.pcap & [1 ...

  3. dubbo-zookeeper quick start

    目录 dubbo快速开始 服务提供者(Service provider) 定义服务接口(Defining service interfaces) 在服务提供方实现接口(Implement interf ...

  4. 使用python模块plotdigitizer抠取论文图片中的数据

    技术背景 对于各行各业的研究人员来说,经常会面临这样的一个问题:有一篇不错的文章里面有很好的数据,但是这个数据在文章中仅以图片的形式出现.而假如我们希望可以从该图片中提取出数据,这样就可以用我们自己的 ...

  5. 面试准备——计算机网络(https)

    一.为什么要提出HTTPS? HTTP的缺点: 明文通信.不加密,可能被窃听. 无身份验证,可能遭遇伪装. 无法证明报文的完整性,可能被篡改. 二.HTTPS = HTTP+加密(防窃听)+认证(防伪 ...

  6. 【Arduino学习笔记08】使用串口监视器显示数据

    代码及相关说明: 1 // 示例:读取模拟输入并显示在串口监视器中 2 3 const int ANALOG_IN = 0; 4 int val = 0; 5 6 void setup(){ 7 Se ...

  7. 免费报表工具 积木报表(JiMuReport)的安装

    分享一b/s报表工具(服务),积木报表(JiMuReport),张代浩大佬出品. 官网:http://www.jimureport.com/ 离线版官方下载:https://github.com/zh ...

  8. 漏洞复现-fastjson1.2.24-RCE

              0x00 实验环境 攻击机:Win 10.Win Server2012 R2(公网环境,恶意java文件所在服务器) 靶机也可作为攻击机:Ubuntu18 (公网环境,docker ...

  9. 泛微OA 多版本存在命令执行

    参考 https://blog.csdn.net/qq_41770175/article/details/102821349 https://www.linktrust.net/oday/471.ht ...

  10. FreeBSD 日常应用

    freebsd日常应用 办公libreoffice或者apache openoffice 设计 图像编辑:gimp 矢量图设计:lnkscape 视频剪辑:openshot 视频特效:natron 编 ...