码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1045. Favorite Color Stripe (30)

时间:2015-08-21 23:02:21      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

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

采用动态规划的算法,记录下每一个从头到每个位置的子字符串的最长序列, 例如:

        2  2  4  1  5  5  6  3  1  1  5  6

2      0  1  2  2  2  2  2  2  2  2  2  2  2
2 3     0  1  2  2  2  2  2  2  3  3  3  3  3
2 3 1    0  1  2  2  3  3  3  3  3  4  5  5  5
2 3 1 5   0  1  2  2  3  4  5  5  5  5  5  6  6
2 3 1 5 6  0  1  2  2  3  4  5  6  6  6  6  6  7

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 vector<int> index[201];
 7 int favoriteColor[201];
 8 int stripe[10001];
 9 int maxLength[201][10000];
10 
11 int FindColor(int color, int column)
12 {
13     vector<int>& vec = index[color];
14     for (int i = 0; i < vec.size(); i++)
15         if (vec[i] <= column)
16             return vec[i];
17     return -1;
18 }
19 int main()
20 {
21     int colorNum, n;
22     cin >> colorNum >> n;
23     for (int i = 0; i < n; i++)
24         cin >> favoriteColor[i];
25     for (int i = n - 1; i >= 0; i--)
26         index[favoriteColor[i]].push_back(i);
27     int length;
28     cin >> length;
29     for (int i = 1; i <= length; i++)
30         cin >> stripe[i];
31 
32     for (int j = 0; j < n; j++)
33         maxLength[j][0] = 0;
34     for (int j = 1; j <= length; j++)
35     {
36         int color = stripe[j];
37         for (int i = 0; i < n; i++)
38         {
39             if (FindColor(color, i) == -1)
40                 maxLength[i][j] = maxLength[i][j - 1];
41             else
42             {
43                 int index = FindColor(color, i);
44                 if (maxLength[index][j - 1] + 1 > maxLength[i][j - 1])
45                     maxLength[i][j] = maxLength[index][j - 1] + 1;
46                 else
47                     maxLength[i][j] = maxLength[i][j - 1];
48             }
49         }
50     }
51 
52     cout << maxLength[n-1][length];
53 }

 

PAT 1045. Favorite Color Stripe (30)

标签:

原文地址:http://www.cnblogs.com/jackwang822/p/4749085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!