博客
关于我
1045 Favorite Color Stripe
阅读量:416 次
发布时间: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/

你可能感兴趣的文章
1045 Favorite Color Stripe
查看>>
B. Spreadsheets(进制转换,数学)
查看>>
等和的分隔子集(DP)
查看>>
基础练习 十六进制转八进制(模拟)
查看>>
L - Large Division (大数, 同余)
查看>>
39. Combination Sum
查看>>
41. First Missing Positive
查看>>
80. Remove Duplicates from Sorted Array II
查看>>
83. Remove Duplicates from Sorted List
查看>>
410. Split Array Largest Sum
查看>>
开源项目在闲鱼、b 站上被倒卖?这是什么骚操作?
查看>>
Vue3发布半年我不学,摸鱼爽歪歪,哎~就是玩儿
查看>>
《实战java高并发程序设计》源码整理及读书笔记
查看>>
Java开源博客My-Blog(SpringBoot+Docker)系列文章
查看>>
程序员视角:鹿晗公布恋情是如何把微博搞炸的?
查看>>
Spring+SpringMVC+MyBatis+easyUI整合进阶篇(七)一次线上Mysql数据库崩溃事故的记录
查看>>
【JavaScript】动态原型模式创建对象 ||为何不能用字面量创建原型对象?
查看>>
ClickHouse源码笔记4:FilterBlockInputStream, 探寻where,having的实现
查看>>
Linux应用-线程操作
查看>>
多态体验,和探索爷爷类指针的多态性
查看>>