• PAT 1045 Favorite Color Stripe(DP)


    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}.

    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:

    1. 6
    2. 5 2 3 1 5 6
    3. 12 2 2 4 1 5 5 6 3 1 1 5 6

    Sample Output:

    7

    总结:这是一道DP题目,而且还是需要转化一下,现在的能力属实不会做,不过这道题目和这道题目是差不多的

    1. #include
    2. using namespace std;
    3. int a[210],b[10010],d[10010];
    4. int main(){
    5. int n,m;
    6. cin >> n >> m;
    7. int x,num=0,maxan=0;
    8. for(int i=1;i<=m;i++){
    9. scanf("%d",&x);
    10. a[x]=i;
    11. }
    12. cin >> m;
    13. for(int i=0;i
    14. scanf("%d",&x);
    15. if(a[x]>=1){
    16. b[num++]=a[x];
    17. }
    18. }
    19. for(int i=0;i
    20. d[i]=1;
    21. for(int j=0;j
    22. if(b[i]>=b[j]) d[i]=max(d[i],d[j]+1);
    23. }
    24. maxan=max(maxan,d[i]);
    25. }
    26. printf("%d",maxan);
    27. return 0;
    28. }

    好好学习,天天向上!

    我要考研

    2022.11.10

    1. #include
    2. using namespace std;
    3. int a[210],b[10010],d[10010];
    4. int main(){
    5. int n,m;
    6. scanf("%d%d",&n,&m);
    7. int num=0,x,maxn=0;
    8. for(int i=1;i<=m;i++){
    9. scanf("%d",&x);
    10. a[x]=i;//存下该种颜色的顺序
    11. }
    12. scanf("%d",&m);
    13. for(int i=0;i
    14. scanf("%d",&x);
    15. if(a[x]>=1)//如果这种颜色是喜欢的颜色
    16. b[num++]=a[x];//按顺序存储每一种颜色的喜欢顺序
    17. }
    18. for(int i=0;i
    19. d[i]=1;
    20. for(int j=0;j
    21. if(b[i]>=b[j])//以第i个颜色结尾,最长的颜色序列的长度
    22. //如果该种颜色的顺序在第j种颜色之后,那么可以将第j个颜色加入i的前面
    23. d[i]=max(d[i],d[j]+1);
    24. maxn=max(maxn,d[i]);
    25. }
    26. printf("%d\n",maxn);
    27. return 0;
    28. }

  • 相关阅读:
    【应用统计学】描述数据分布集中趋势
    《canvas》之第3章 曲线图形
    C++学习笔记(六)——日期类的实现
    LeetCode 623. 在二叉树中增加一行
    实用篇-认识微服务
    c++学习之 继承的方式
    企业低成本万能架构
    Linux系统下利用共享内存模拟迅雷下载
    MATLAB中M文件编写
    Acwing第76场周赛
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127038540