1.这个和最长公共子序列问题相类似(LCS),给出目标字符串,求出在t中求出符合顺序和颜色要求的最长子字符串(可以省去目标字符串中的一些颜色)。
2.不同的地方是允许元素重复,如{a}和{aaa},匹配出来的是3,a可以重复3次。
3.该问题一开始卡在了输入格式上。
4.动态规划方程:dp[i][j]表示f[0~i]与o[0~j]匹配的最大长度。
如果f[i]==o[j],dp[i][j]=max(dp[i][j-1]+1,dp[i-1][j-1]),当目前颜色相同,在上一个的基础上+1(表示上一次已经使用f[i]进行匹配),或者在dp[i-1][j-1]上+1(表示这次才开始使用f[i]作为匹配);
如果f[i]!=o[j],dp[i][j]=max(dp[i][j-1],dp[i-1][j]),当目前的颜色不相同,那么选取dp[i][j-1](f[i]已经进行匹配)和dp[i-1][j](f[i]未进行匹配)的最大值。
5.还有一种最简单的方法:dp[i][j]=max(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]),如果f[i]==o[j],则dp[i][j]++:(采用了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.
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
简单方法的AC代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
6
5 2 3 1 5 6
10 2 2 3 1 3 1 3 1 3 1
6
5 2 3 1 5 6
10
5 6 6 6 6 6 6 6 6 6 6
6
5 2 3 1 5 6
10
2 6 6 6 6 6 6 6 6 6 6
6
5 2 3 1 5 6
10
2 2 2 6 6 6 6 6 6 6 6
6
5 2 3 1 5 6
10
2 2 2 6 6 6 6 5 6 6 6
6
5 2 3 1 5 6
10
2 2 2 6 6 6 6 6 5 6 6
*/
int main(void)
{
int colourSum,fnum, onum;
cin >> colourSum>> fnum;
vector<int> f(fnum);
for (int i = 0; i<fnum; i++)
{
scanf("%d", &f[i]);
}
cin >> onum;
vector<int> o(onum);
for (int i = 0; i<onum; i++)
{
scanf("%d", &o[i]);
}
vector<vector<int>> dp(fnum+1, vector<int>(onum+1,0));
int thisMax = 0;
for (int i = 1; i<=f.size(); i++)
{
for (int j = 1; j<=o.size(); j++)
{
dp[i][j] = max(max(dp[i – 1][j], dp[i][j – 1]), dp[i-1][j-1]);
if (f[i-1] == o[j-1])
dp[i][j]++;
}
}
cout << dp[fnum][onum] << endl;
return 0;
}
[/c]
第一种方法的AC代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
6
5
2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
4
5
2 3 1 5 6
10 2 2 3 1 3 1 3 1 3 1
2
5
2 3 1 5 6
10
5 6 6 6 6 6 6 6 6 6 6
2
5
2 3 1 5 6
10
2 6 6 6 6 6 6 6 6 6 6
2
5
2 3 1 5 6
10
2 2 2 6 6 6 6 6 6 6 6
*/
int main(void)
{
int colorSum,fnum, onum;
cin >> colorSum>> fnum;
vector<int> f(fnum);
for (int i = 0; i<fnum; i++)
{//输入喜欢的颜色
scanf("%d", &f[i]);
}
cin >> onum;
vector<int> o(onum);
for (int i = 0; i<onum; i++)
{//输入源材料
scanf("%d", &o[i]);
}
vector<vector<int>> dp(fnum, vector<int>(onum));
for (int i = 0; i<f.size(); i++)
{//初始化边界数组
if (f[i] == o[0])
dp[i][0] = 1;
else if (i == 0)
dp[i][0] = 0;
else
dp[i][0] = dp[i – 1][0];
}
for (int i = 0; i<f.size(); i++)
{
for (int j = 1; j<o.size(); j++)
{
if (f[i] == o[j])
{//如果相等,那么dp[i][j]由dp[i][j-1]和dp[i-1][j-1]的最大值构成
if (i != 0)
dp[i][j] = max(dp[i][j – 1] + 1, dp[i – 1][j – 1] + 1);
else
dp[i][j] = dp[i][j – 1] + 1;
}
else
{
if (i != 0)
dp[i][j] = max(dp[i][j – 1], dp[i – 1][j]);//取包含上一个f的长度和不包含上一个f的长度两者最大值
else
dp[i][j] = dp[i][j – 1];
}
}
}
cout << dp[fnum – 1][onum – 1] << endl;
return 0;
}
[/c]