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

 



上一篇:PAT 甲级 1045 Favorite Color Stripe


下一篇:Japan's hybrid model inventory of hair Ewha Rinka