【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

题意:

输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个正整数L(<=10000),代表条带的长度,接着输入L个正整数表示条带上的颜色的编号。输出以喜爱颜色顺序排列的最长子串长度(不必每种颜色都有,只保证相对位置相同,同种颜色可连续)。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[10007],pos[207];
int dp[10007];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
int color;
for(int i=1;i<=m;++i){
cin>>color;
pos[color]=i;//记录颜色的相对位置
}
int cnt=0;
int stripe;
int l;
cin>>l;
for(int i=1;i<=l;++i){
cin>>stripe;
if(pos[stripe])
a[++cnt]=pos[stripe],dp[cnt]=1;//记录条带上颜色在喜爱条带上的位置
}
int mx=0;
for(int i=1;i<=cnt;++i){
for(int j=1;j<i;++j)//以相对位置j结尾的都可以在后面加上以相对位置i结尾的颜色
if(a[j]<=a[i])
dp[i]=max(dp[i],dp[j]+1);//更新以相对位置i结尾的子串的长度
mx=max(mx,dp[i]);//选取最长长度作为答案
}
cout<<mx;
return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/11605819.html

时间: 2024-11-03 21:21:40

【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)的相关文章

PAT 甲级 1045 Favorite Color Stripe(DP)

题目链接 Favorite Color Stripe 题意:给定A序列和B序列,你需要在B序列中找出任意一个最长的子序列,使得这个子序列也是A的子序列 (这个子序列的相邻元素可以重复) 只要求出这个子序列长度的最大值即可 很基础的DP题,设f[i]为当前以a[i]结尾的子序列的长度的最大值,扫描B序列的每个元素时实时更新即可. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i

PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

1068 Find More Coins (30 分)   Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special

PAT 1045. Favorite Color Stripe (30)

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

1045. Favorite Color Stripe (30)

LCS 最大公共子序列. memset函数需要include <string.h> 时间限制 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 cut

1045. Favorite Color Stripe (30) -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.

PAT甲级——1131 Subway Map (30 分)

可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing

PAT 甲级 1014 Waiting in Line (30 分)(queue的使用,模拟题,有个大坑)

1014 Waiting in Line (30 分) Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are: The space inside the yellow line

PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs case 7 过不了求助!!!)

1018 Public Bike Management (30 分)   There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Pub

【PAT甲级】1103 Integer Factorization (30分)

1103 Integer Factorization (30分) The K?P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K?P factorization of N for any positive integers N, K an