zoukankan      html  css  js  c++  java
  • PAT甲级——A1045 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 (≤) 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 (≤) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤) 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

    LIS——最长不下降子序列:
    算法设计
    定义一个长度为N的order数组,将Eva喜欢的颜色编号作为下标,对应元素为次序编号,例如给定喜欢的颜色为2 3 1 5 6,那么order[2]=1,order[3]=2……定义数组A存储给定的珠子序列,定义一个数组dp,其中dp[i]表示以 A[i]结尾的符合要求的子序列最长长度。那么对于0<=i<N,找出0<=j<i之间满足order[i]>=order[j]的最大的dp元素值MAX,则dp[i]=max(MAX,1)。遍历dp数组找出最大的值即为所求。

     1 #include <iostream>
     2 using namespace std;
     3 int like[205], color[10010], dp[10010];
     4 int main()
     5 {
     6     int n, m, k, res = 0, x, nums = 0;
     7     cin >> n >> m;
     8     for(int i=1;i<=m;++i)//给自己喜欢颜色排序标号
     9     {
    10         cin >> x;
    11         like[x] = i;        
    12     }
    13     cin >> k;
    14     for (int i = 0; i < k; ++i)
    15     {
    16         cin >> x;
    17         if (like[x] > 0)//删除不是喜欢颜色的变量
    18             color[nums++] = like[x];//记住该颜色的标号
    19     }
    20     for (int i = 0; i < nums; ++i)
    21     {
    22         dp[i] = 1;//每种颜色单独拼接的长度为1
    23         for (int j = 0; j < i; ++j)
    24             if (color[j] <= color[i])//只要前面的颜色的序号比自己小,那么就可以和直接拼接
    25                 dp[i] = dp[i] > (dp[j] + 1) ? dp[i] : (dp[j] + 1);
    26         res = res > dp[i] ? res : dp[i];
    27     }
    28     cout << res;
    29     return 0;
    30 }

    LCS——最长公共子序列
    算法设计
    定义一个数组A储存喜欢的颜色编号序列,定义一个数组B存储给定的一串珠子序列,找出A、B之间的最长公共子序列即可。由于颜色可以重复,即子序列中可以有重复元素,状态转移方程为:

    如果A[i]==B[j],

    如果A[i]!=B[j],

    边界情况为:

    dp[i][0]=dp[0][j]=0(0<=i<=M,0<=j<=L)

     1 using namespace std;
     2 int dp[205][10005];
     3 int main(){
     4     int N,M,L;
     5     scanf("%d%d",&N,&M);
     6     int A[M+1]={0};
     7     for(int i=1;i<=M;++i)
     8         scanf("%d",&A[i]);
     9     scanf("%d",&L);
    10     int B[L+1]={0};
    11     for(int i=1;i<=L;++i)
    12         scanf("%d",&B[i]);
    13     for(int i=1;i<=M;++i)
    14         for(int j=1;j<=L;++j)
    15             if(A[i]==B[j])
    16                 dp[i][j]=max(dp[i][j-1],dp[i-1][j-1])+1;
    17             else
    18                 dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
    19     printf("%d",dp[M][L]);
    20     return 0;
    21 }



  • 相关阅读:
    Qt之使用CQU库快速开发统一风格界面
    Springboot中使用redis进行api防刷限流
    SpringBoot使用拦截器、过滤器、监听器
    ElasticSearch中文分词器-IK分词器的使用
    里式替换原则——面向对象程序设计原则
    使用Spring中的PropertyPlaceholderConfigurer读取文件
    新版本SpringCloud sleuth整合zipkin
    解放双手,在PC端进行Android真机调试
    破解Android设备无法联调的谜题
    打python&adb组合拳,实现微信读书永久免费读
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11273153.html
Copyright © 2011-2022 走看看