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 }



  • 相关阅读:
    JavaScript入门知识点整理
    正则表达式
    bootstrap css编码规范
    JavaScript高级编程(学习笔记)
    【 D3.js 选择集与数据详解 — 2 】 使用data()绑定数据
    bootstrap table:JQuery中each方法绑定blur事件监听input输入是否合法,进入死循环
    bootstrap-table中导出excel插件bootstrap-table-export使用
    托业考后感
    《Pride and Prejudice》英文版读后记忆
    迷茫的当下,我在做什么
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11273153.html
Copyright © 2011-2022 走看看