1045 Favorite Color Stripe
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的更多相关文章
- 1045 Favorite Color Stripe 动态规划
1045 Favorite Color Stripe 1045. Favorite Color Stripe (30)Eva is trying to make her own color strip ...
- 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. ...
- 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 ) (动态规划 )
1045 Favorite Color Stripe (30 分) Eva is trying to make her own color stripe out of a given one. She ...
- 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 ...
- PAT 甲级 1045 Favorite Color Stripe
https://pintia.cn/problem-sets/994805342720868352/problems/994805437411475456 Eva is trying to make ...
- 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 ...
- 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 ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
随机推荐
- QT现场同步
// 1线程同步 QFutureSynchronizer<void> synchronizer; //2线程1 synchronizer.addFuture(QtConcurrent::r ...
- 后端程序员之路 24、Redis hiredis
Redishttps://redis.io/ Redis快速入门 - Redis教程http://www.yiibai.com/redis/redis_quick_guide.html wget ht ...
- 测试平台系列(2) 给Pity添加配置
给Pity添加配置 回顾 还记得上篇文章创立的「Flask」实例吗?我们通过这个实例,给根路由 「/」 绑定了一个方法,从而使得用户访问不同路由的时候可以执行不同的方法. 配置 要知道,在一个「Web ...
- windows 下使用vargant 搭建虚拟机服务
使用vagrant 下载 vagrant[https://www.vagrantup.com/downloads.html] 下载管理工具VirtualBox[https://www.virtualb ...
- 比较String 字符串的字节大小
package com.ittx.edi.erp;import java.io.File;import java.io.FileWriter;import java.io.IOException;pu ...
- Idea 报错 xxxx too long
问题:写单元测试,debug时,报错如下图 解决方法1: 在项目/.idea/workspace.xml文件中添加一行代码如下 <component name="PropertiesC ...
- Python字典与集合
一 字典创建.访问.添加.删除.修改.内建函数.内建方法 创建,列表不能作为键,因为键不能变?字典也不能作为键 dict1 = {} dict2 = {'name':'qq','sex':'male' ...
- VS添加dll引用
直接添加(CADImport.dll) 手动添加 (sgcadexp.dll) 直接放到项目bin的目录下
- Hibernate学习实例
一 Hibernate简介 Hibernate是一种Java语言下的对象关系映射(ORM)解决方案.为面向对象的领域模型到传统的关系型数据库的映射提供了一个使用方便的框架. 二 Hibernate设计 ...
- 01-静态web服务器(Python)-面向对象的对比
普通写法,静态web服务器: 先创建TCP服务器套接字,然后等待客户端(这里是浏览器)请求连接. 客户端发起请求,用线程来处理连接的建立,这样可以实现多任务(也就是并发) 连接后根据请求发送指定页面 ...