PAT Advanced 1045. Favorite Color Stripe (30) (C语言实现)
题目
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$ ( $\le 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$ ( $\le 200$ ) followed by $M$ Eva’s favorite color numbers given in her favorite order. Finally the third line starts with a positive integer $L$ ( $\le 10^4$ ) 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:
1
2
3
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
1
7
思路
这道题的大意即给出了一组颜色的排序,在一长串颜色中按该顺序选取其中一个子数列。
其实,颜色的数字大小并无意义,如果将所给颜色重新映射为颜色的排名,对于题目的例子,即2->5, 3->4, 1->3, 5->2, 6->1, 4->0
(不在喜爱颜色里的,如4,就映射为0),那么相当于在一个数组里找出最长非递减子列。上面这句话是最重要的,是最重要的,是最重要的。
我比较笨,好长时间都想不出一个快速的方法,终于一天开窍了。这个思路是一遍过的,随时得到目前已读数据的最终解,空间复杂度O(N),时间复杂度O(LN)。
打了很多字,感觉还是说的少一点,更容易理解,所以一句话:
截至某个元素的最长非递减子列长度,等于它之前截至排名更高的任何颜色(注意不是任何元素)的最长非递减子列长度+1。
伪代码:
- 读取N和M个喜爱颜色,排序
- 初始化截止于每一个颜色的最长非递减子列长度为0
- 读取L个颜色,对每一个颜色
- 遍历排名高于或等于该颜色的所有颜色,找出子列长度的最大值
- 将该颜色的最长子列长度更新为上述最长长度+1
- 遍历所有颜色,输出子列最长者
代码
Github最新代码,欢迎交流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
int main()
{
int N, M, L, c, order_to_color[200] = {0}, color_to_order[200] = {0};
int longest_for_color[200] = {0}, longest_before_current, max_length = 0;
/* read number of different colors */
scanf("%d", &N);
/* read color-order pairs (colors start from 0, orders start from 1) */
scanf("%d", &M);
for (int i = M; i > 0; i--) {
scanf("%d", &c);
order_to_color[i] = --c;
color_to_order[c] = i;
}
/* read color list */
scanf("%d", &L);
for (int i = 0; i < L; i++) {
scanf("%d", &c);
/* this color is not in the favourite list, do nothing */
if (color_to_order[--c] == 0)
continue;
/* for colors of higher/equal order, find the longest sub-stripe */
longest_before_current = 0;
for (int j = M; j >= color_to_order[c]; j--)
if (longest_before_current < longest_for_color[order_to_color[j]])
longest_before_current = longest_for_color[order_to_color[j]];
/* after this color, the length increases by 1 */
longest_for_color[c] = longest_before_current + 1;
}
for (int i = 0; i < N; i++)
if (max_length < longest_for_color[i])
max_length = longest_for_color[i];
printf("%d\n", max_length);
return 0;
}