1045 Favorite Color Stripe
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}.
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.
For each test case, simply print in a line the maximum length of Eva's favorite stripe.
- 6
- 5 2 3 1 5 6
- 12 2 2 4 1 5 5 6 3 1 1 5 6
7
总结:这是一道DP题目,而且还是需要转化一下,现在的能力属实不会做,不过这道题目和这道题目是差不多的
- #include
- using namespace std;
- int a[210],b[10010],d[10010];
-
- int main(){
- int n,m;
- cin >> n >> m;
-
- int x,num=0,maxan=0;
- for(int i=1;i<=m;i++){
- scanf("%d",&x);
- a[x]=i;
- }
-
- cin >> m;
- for(int i=0;i
- scanf("%d",&x);
- if(a[x]>=1){
- b[num++]=a[x];
- }
- }
-
- for(int i=0;i
- d[i]=1;
- for(int j=0;j
- if(b[i]>=b[j]) d[i]=max(d[i],d[j]+1);
- }
- maxan=max(maxan,d[i]);
- }
-
- printf("%d",maxan);
-
- return 0;
- }
好好学习,天天向上!
我要考研!
2022.11.10
- #include
- using namespace std;
- int a[210],b[10010],d[10010];
-
- int main(){
- int n,m;
- scanf("%d%d",&n,&m);
-
- int num=0,x,maxn=0;
- for(int i=1;i<=m;i++){
- scanf("%d",&x);
- a[x]=i;//存下该种颜色的顺序
- }
-
- scanf("%d",&m);
- for(int i=0;i
- scanf("%d",&x);
- if(a[x]>=1)//如果这种颜色是喜欢的颜色
- b[num++]=a[x];//按顺序存储每一种颜色的喜欢顺序
- }
-
- for(int i=0;i
- d[i]=1;
- for(int j=0;j
- if(b[i]>=b[j])//以第i个颜色结尾,最长的颜色序列的长度
- //如果该种颜色的顺序在第j种颜色之后,那么可以将第j个颜色加入i的前面
- d[i]=max(d[i],d[j]+1);
- maxn=max(maxn,d[i]);
- }
-
- printf("%d\n",maxn);
- return 0;
- }
-
-
相关阅读:
webpack依赖包是同一个仓库,也要进行安装
数据结构-堆排序Java实现
c 理解创建多进程
【mysql】—— 复合查询
NTP时间同步
SSO单点登录
一同走进Linux的“基操”世界
ChatGLM-6B+LangChain与训练及模型微调教程
PythonStudy5
Hive学习笔记2
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/127038540