1045 Favorite Color Stripe (最长不下降子序列 LIS)

Posted exciting

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1045 Favorite Color Stripe (最长不下降子序列 LIS)相关的知识,希望对你有一定的参考价值。

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

  

一开始没审清题意,这里喜欢的颜色序列是可以不全部出现的,所以只有27/30分:

#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
    int index=1;
    int data=1;
}c[10010];
int a[220];
int b[10010];

int main(){
    int n,m,k;
    scanf("%d",&n);
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
        scanf("%d",&a[i]);
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
        scanf("%d",&b[i]);
    int maxx=-1,data,index,i,temp;
    for(i=1;i<=k;i++){
        if(b[i]==a[1]){
            temp=i;
            break;
        }
    }
    for(i=temp+1;i<=k;i++){
        for(int j=temp;j<i;j++){
            index=c[j].index;
            data=c[j].data;
            if(index==n){
                if(b[i]==a[index]&&(data+1>c[i].data)){
                    c[i].data=data+ 1;
                    c[i].index=index;
                }    
            }else if(b[i]==a[index]){
                if(data+1>c[i].data){
                    c[i].data=data+1;
                    c[i].index=index;
                }
            }else if(b[i]==a[index+1]){
                if(data+1>c[i].data){
                    c[i].data=data+1;
                    c[i].index=index+1;
                }
            }
            if(c[i].data>maxx)
                maxx=c[i].data;
        }
    }
    printf("%d",maxx);
    return 0;
}

 

而且这种思路较为复杂,可以将喜欢的颜色通过hash按顺序映射到一个递增序列,即可方便转化为LIS问题:

#include<cstdio>
#include<algorithm>
using namespace std;
int dp[10010];
int b[10010];
int hash[220]={0};
int main(){
    int n,m,k,temp,now=1,now1=1,Max=-1;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d",&temp);
        hash[temp]=now++;
    }
    scanf("%d",&k);
    for(int i=1;i<=k;i++){
        scanf("%d",&temp);
        if(hash[temp]!=0){
            b[now1++]=temp;
        }
    }
    for(int i=1;i<now1;i++){
        dp[i]=1;
        for(int j=1;j<i;j++)
        if(hash[b[j]]<=hash[b[i]]&&dp[j]+1>dp[i]){
            dp[i]=dp[j]+1;
        }
        if(dp[i]>Max) Max=dp[i];
    }
    printf("%d",Max);
    return 0;
}

 

考虑使用LCS,这里的特殊点是公共部分可以存在重复元素,故对原模型进行修改

得出递推式

dp[i][j]=max(dp[i][j-1],dp[i-1][j])+1            如果a[i]等于b[j]的话

dp[i][j]=max(dp[i][j-1],dp[i-1][j])                  a[i]!=b[j]的话

#include<cstdio>
#include<algorithm>
using namespace std;
int a[220];
int b[10010];
int dp[220][10010];

int main(){
    int n,lenA,lenB,temp;
    scanf("%d%d",&n,&lenA);
    for(int i=1;i<=lenA;i++)
        scanf("%d",&a[i]);
    scanf("%d",&lenB);
    for(int i=1;i<=lenB;i++)
        scanf("%d",&b[i]);
    for(int i=0;i<=lenA;i++)
        dp[i][0]=0;
    for(int j=0;j<=lenB;j++)
        dp[0][j]=0;
    for(int i=1;i<=lenA;i++){
        for(int j=1;j<=lenB;j++){
            if(a[i]==b[j])
                dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
            else
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
    }
    printf("%d",dp[lenA][lenB]);
    return 0;
}

 

以上是关于1045 Favorite Color Stripe (最长不下降子序列 LIS)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1045. Favorite Color Stripe

PAT 1045. Favorite Color Stripe (30)

1045. Favorite Color Stripe (30)-PAT甲级真题

1045 Favorite Color Stripe (最长不下降子序列 LIS)

[pat]1045 Favorite Color Stripe

1045 Favorite Color Stripe(LIS解法)