博客
关于我
1045 Favorite Color Stripe
阅读量:430 次
发布时间:2019-03-06

本文共 2668 字,大约阅读时间需要 8 分钟。

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:

65 2 3 1 5 612 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 
2 3 using namespace std; 4 5 int main() { 6 int n, m, l; 7 cin >> n >> m; 8 vector
colors(n + 1); 9 set
found;10 for (int i = 1; i <= m; ++i) cin >> colors[i];11 cin >> l;12 vector
stripe(l + 1);13 for (int i = 1; i <= l; ++i) cin >> stripe[i];14 vector
> dp(n + 1, vector
(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 }

 

转载地址:http://lstuz.baihongyu.com/

你可能感兴趣的文章
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>
MySQL 的instr函数
查看>>
MySQL 的mysql_secure_installation安全脚本执行过程介绍
查看>>
MySQL 的Rename Table语句
查看>>
MySQL 的全局锁、表锁和行锁
查看>>
mysql 的存储引擎介绍
查看>>
MySQL 的存储引擎有哪些?为什么常用InnoDB?
查看>>
Mysql 知识回顾总结-索引
查看>>
Mysql 笔记
查看>>
MySQL 精选 60 道面试题(含答案)
查看>>
mysql 索引
查看>>
MySQL 索引失效的 15 种场景!
查看>>
MySQL 索引深入解析及优化策略
查看>>