1045. Favorite Color Stripe (30)

LCS 最大公共子序列。 memset函数需要include <string.h>

时间限制

200 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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 (<=10000) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line are 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

来源: <http://www.patest.cn/contests/pat-a-practise/1045>

  1. #include<iostream>
  2. #include<stdio.h>
  3. #pragma warning(disable:4996)
  4. using namespace std;
  5. int like[201], given[10001], mp[201][10001];
  6. int main(void) {
  7. memset(mp, 0, sizeof(mp));
  8. //freopen("Text.txt", "r", stdin);
  9. int n;
  10. cin >> n;
  11. int m;
  12. cin >> m;
  13. for (int i = 1; i <= m; i++) {
  14. cin >> like[i];
  15. }
  16. int p;
  17. cin >> p;
  18. for (int i = 1; i <= p; i++) {
  19. cin >> given[i];
  20. }
  21. for (int i = 1; i <= m; i++) {
  22. for (int j = 1; j <= p; j++) {
  23. int max = mp[i - 1][j - 1];
  24. if (mp[i - 1][j]>max)
  25. max = mp[i - 1][j];
  26. if (mp[i][j - 1]>max)
  27. max = mp[i][j - 1];
  28. if (given[j] == like[i])
  29. max += 1;
  30. mp[i][j] = max;
  31. }
  32. }
  33. cout << mp[m][p];
  34. return 0;
  35. }

来自为知笔记(Wiz)

时间: 2024-08-24 19:35:04

1045. Favorite Color Stripe (30)的相关文章

PAT 1045. Favorite Color Stripe (30)

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

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 favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个正整数L(<=10000),代表条带的长度,接着输入L个正整数表示条带上的颜色的编号.输出以喜爱颜色顺序排列的最长子串长度(不必每种颜色都有,只保证相对位置相同,同种颜色可连续). 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>

pat1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 t

1045 Favorite Color Stripe (最长不下降子序列 LIS)

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

PAT 甲级 1045 Favorite Color Stripe(DP)

题目链接 Favorite Color Stripe 题意:给定A序列和B序列,你需要在B序列中找出任意一个最长的子序列,使得这个子序列也是A的子序列 (这个子序列的相邻元素可以重复) 只要求出这个子序列长度的最大值即可 很基础的DP题,设f[i]为当前以a[i]结尾的子序列的长度的最大值,扫描B序列的每个元素时实时更新即可. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i

PAT 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

codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同. 解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到. 解题代码: 1 // File Name: 219c.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月26日 星期六 15时45分56秒 4 5 #include<vector> 6 #include<list>

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