您的位置:首页 > 其它

1045. Favorite Color Stripe (30),需要抽象问题

2016-11-26 13:50 288 查看

题目地址

https://www.patest.cn/contests/pat-a-practise/1045

题目描述

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)

DFS回溯,深度太大,会超时(可以得到24分,写法很简单)

采用 类最长递增子序列问题 求解 有个case过不了

主要是重复元素的问题??

采用 类最长公共子序列(LCS)求解 ,可以ac

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <map>
#include <set>

using namespace std;

const int INF = 0x7fffffff;
const int MIN_INF = - INF -1;
typedef long long int LL;

int n;
int m;
int l;
int favorite[201]; // 颜色有优先级

int main()
{
freopen("in.txt","r",stdin);

scanf("%d", &n);

vector<int> vs;
vs.clear();
scanf("%d",&m);
for(int i=1;i<201;i++)
{
favorite[i] = -1;
}
int tmp;

for(int i=1;i<=m;i++)
{
scanf("%d", &tmp);
favorite[tmp] = i;
}

scanf("%d", &l);
for(int i=0;i<l;i++)
{
scanf("%d",&tmp);
if(favorite[tmp] != -1)
{
vs.push_back(favorite[tmp]);
}
}

int len = vs.size();
vector<int> fa(m);
for(int i=0;i<m;i++)
{
fa[i] = i + 1;
}

vector<vector<int>> dp(len + 1, vector<int>(m + 1,0));
for(int i = 0;i<len;i++)
{
for(int j = 0;j<m;j++)
{
dp[i+1][j+1] = max(max(dp [i][j+1],dp[i+1][j]),dp[i+1][j+1]);
if(vs[i] == fa[j])
dp[i+1][j+1] ++;
}
}
printf("%d\n",dp[len][m]);

return 0;
}


24分的代码,采用类似最长递增子序列问题求解

#include <cstdio>
#include <memory>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <list>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define INF 0x7fffffff

const int N = 205;

int favor
; // 每个数的优先级,权值大的数大

int main()
{
//freopen("in.txt", "r", stdin);
int n;
while(scanf("%d", &n) != EOF)
{
for(int i=1;i<n;i++)
{
favor[i] = -1;
}

int m;
int tmp;
scanf("%d", &m);
for(int i=0;i<m;i++)
{
scanf("%d", &tmp);
favor[tmp] = i + 1;
}
vector<int> stripe;
int L;
scanf("%d", &L);
for(int i=0;i<L;i++)
{
scanf("%d",&tmp);
if(favor[tmp] != -1)
stripe.push_back(favor[tmp]);
}

vector<int> h(10005); // 遍历到某个数时,长度为i+1的最小末尾
int hindex = 0;
h[0] = stripe[0];

int len = stripe.size();
for(int i=1;i<len;i++)
{
if(stripe[i] >= h[hindex])
{
h[++hindex] = stripe[i];
}else{
int j = 0;
while(h[j] < stripe[i]){
j ++;
}
h[j] =  stripe[i];
}
}
printf("%d\n", hindex + 1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: