分析:
完整 代码:
// 最长不下降子序列
#include <stdio.h>
#include <algorithm>
using namespace std; const int N = ;
int A[N], dp[N]; int main()
{
freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++){
scanf("%d", &A[i]);
} int ans = -; // 记录最长的dp[i]
for (int i = ; i <= n; i++){ // 按顺序计算出dp[i]的值
dp[i] = ; // 边界初始条件(先假设每个元素自成一个子序列)
// 如果A[i] >= A[j] 且 A[i]的加入能使dp[i]变长,即dp[j] + 1 > dp[i]
for (int j = ; j < i; j++){
if (A[i] >= A[j] && (dp[j] + > dp[i])){
dp[i] = dp[j] + ; // 状态转移方程,用以更新dp[i]
}
}
ans = max(ans, dp[i]);
} printf("%d", ans);
fclose(stdin); return ;
}
题型实战:
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 (≤104) 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:
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
7
分析:将喜欢的颜色映射到一个非递减序列,然后将所有输入的颜色且是喜欢的颜色映射到一个数组中,求这个数组中最长的非递减序列长度
代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; const int maxc = ; // 最大颜色数
const int maxn = ; // 最大的L //
int HashTable[maxc]; // 将喜欢的颜色映射为递增序列,不喜欢的颜色映射为-1
int A[maxn], dp[maxn]; // 最长不下降子序列的原数组A和DP数组 int main()
{
int n, m, x;
scanf("%d%d", &n, &m);
memset(HashTable, -, sizeof(HashTable)); // 将整数HashTable数组初始化为-1
for (int i = ; i < m; i++){
scanf("%d", &x);
HashTable[x] = i;
} int L, num = ; // num存放L个颜色中包含喜欢的颜色的数量
scanf("%d", &L);
for (int i = ; i < L; i++){
scanf("%d", &x);
if (HashTable[x] != -){
A[num++] = HashTable[x];
}
} // 以下为LIS问题的模板
int ans = -;
for (int i = ; i < num; i++){
dp[i] = ;
for (int j = ; j < i; j++){
if (A[j] <= A[i] && dp[j] + > dp[i]){
dp[i] = dp[j] + ;
}
}
ans = max(ans, dp[i]);
} printf("%d\n", ans);
return ;
}