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 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
第一个数没用
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月09日 16:50 | 答案正确 | 30 | 1045 | C++ (g++ 4.7.2) | 11 | 436 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
答案正确 | 1 | 300 | 15/15 | |
1 | 答案正确 | 1 | 308 | 3/3 |
2 | 答案正确 | 1 | 308 | 6/6 |
3 | 答案正确 | 11 | 436 | 3/3 |
4 | 答案正确 | 11 | 436 | 3/3 |
#include<iostream>
#include<vector>
using namespace std;
void readln(vector<int>*Which, int Total)
{
for (int index = 0; index < Total; index++)
cin >> (*Which)[index];
}
void StarCLear(vector<int> *PREmax, int column)
{
while (column--)(*PREmax)[column] = 0;
}
void copyNowToPRE(vector<int> *PREmax, int column, vector<int> * Nowmax)
{
while (column--)(*PREmax)[column] = (*Nowmax)[column];
}
int main()
{
int colorTotal,favourTotal,stripeTotal,max;
cin >> colorTotal>>favourTotal;
vector<int>favorColors(favourTotal);
readln(&favorColors, favourTotal);
cin >> stripeTotal;
vector<int>stripes(stripeTotal);
readln(&stripes, stripeTotal);
favourTotal++;
stripeTotal++;
vector<int>PREmax(stripeTotal), Nowmax(stripeTotal);
StarCLear(&PREmax, stripeTotal);
for (int usecolor = 1; usecolor <favourTotal; usecolor++)/*颜色从i=0~favourable-1;而对应颜色i=usecolor-1最多的放在maxdrix[draw]*/
{
for (int draw =1; draw < stripeTotal; draw++)
{
max = PREmax[draw]>Nowmax[draw - 1] ? PREmax[draw] : Nowmax[draw - 1];
Nowmax[draw] = stripes[draw - 1] == favorColors[usecolor - 1] ? max + 1 : max;
}
copyNowToPRE(&PREmax, stripeTotal, &Nowmax);
}
cout << Nowmax[stripeTotal -1] << endl;;
system("pause");
return 0;
}
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月09日 16:28 | 答案正确 | 30 | 1045 | C++ (g++ 4.7.2) | 13 | 8372 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
答案正确 | 1 | 436 | 15/15 | |
1 | 答案正确 | 1 | 436 | 3/3 |
2 | 答案正确 | 1 | 436 | 6/6 |
3 | 答案正确 | 12 | 8372 | 3/3 |
4 | 答案正确 | 13 | 8372 | 3/3 |
#include<iostream>
#include<vector>
using namespace std;
void readln(vector<int>*Which, int Total)
{
for (int index = 0; index < Total; index++)
cin >> (*Which)[index];
}
void StarCLear(vector<vector<int>> *maxdrix, int row, int column)
{
while (row--)(*maxdrix)[row][0] = 0;
while (column--)(*maxdrix)[0][column] = 0;
}
int main()
{
int colorTotal,favourTotal,stripeTotal,max;
cin >> colorTotal>>favourTotal;
vector<int>favorColors(favourTotal);
readln(&favorColors, favourTotal);
cin >> stripeTotal;
vector<int>stripes(stripeTotal);
readln(&stripes, stripeTotal);
favourTotal++;
stripeTotal++;
vector<vector<int>>maxdrix(favourTotal , vector<int>(stripeTotal ));
StarCLear(&maxdrix, favourTotal, stripeTotal);
for (int usecolor = 1; usecolor <favourTotal; usecolor++)/*颜色从i=0~favourable-1;而对应颜色i=usecolor-1最多的放在maxdrix[usecolor][draw]*/
{
for (int draw =1; draw < stripeTotal; draw++)
{
max = maxdrix[usecolor-1][draw] > maxdrix[usecolor][draw-1] ? /*【这前一种颜色画到此处draw】与【下一个颜色画到这一处前一处】那个更长*/
maxdrix[usecolor-1][draw] : maxdrix[usecolor][draw-1] ;
maxdrix[usecolor][draw]= stripes[draw-1 ] == favorColors[usecolor-1] ?
max + 1 : max;
}
}
cout << maxdrix[favourTotal-1][stripeTotal-1] << endl;;
system("pause");
return 0;
}