1045. Favorite color stripe (30)-pat

Source: Internet
Author: User
1045. Favorite color stripe (30) Time Limit 200 ms memory limit 32000 kb code length limit 16000 B discriminant program standard author Chen, Yue

Eva is trying to make her own color stripe out of a given one. she wowould 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 cocould be very long, and Eva wowould 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 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 5 6}, {2 2 1 5 5 5 6}, {2 2 2 1 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 (<= 10000) which is the length of the given stripe, followed by L colors on the stripe. all the numbers in a line a separated
A space.

Output specification:

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

Sample input:

65 2 3 1 5 612 2 2 4 1 5 5 6 3 1 1 5 6

Sample output:

7
Recommendation index :※※※
Source: http://pat.zju.edu.cn/contests/pat-a-practise/1045dp? I =1_max (DP [1], DP [2],..., DP [J]) + 1, where (j <I)
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>using namespace std;#define max(a,b) ((a)>(b)?(a):(b))int n,m,len;int find_color(const int *fa,const int *stp,int *dp){int i,j;for(i=1;i<=len;i++){int submax=0;for(j=1;j<i;j++){//find a maxsub seq if(fa[stp[i]]>=fa[stp[j]])//check color seq is fitif(dp[j]>submax)submax=dp[j];}dp[i]=submax+1;}return dp[len];}int main(){scanf("%d",&n);int *fa=new int[n+1];memset(fa,0,(n+1)*sizeof(int));scanf("%d",&m);int *arr_fa=new int[m+1];int i,tmp;for(i=1;i<=m;i++){scanf("%d",&tmp);fa[tmp]=i;}scanf("%d",&len);int *stp=new int[len+1];int count=0;for(i=1;i<=len;i++){scanf("%d",&tmp);if(fa[tmp]!=0)stp[++count]=tmp;}len=count;int *dp=new int[len+1];for(i=0;i<=len;i++)dp[i]=1;int maxstp=find_color(fa,stp,dp);printf("%d\n",maxstp);return 0;}
A Recursion is also written, and two cases have timed out.
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>using namespace std;#define max(a,b) ((a)>(b)?(a):(b))#define N 201int n,m,len;int find_color(int curr,int index_stp,int index_fa,const int *fa,const int *stp){if(index_fa>m||index_stp>len)return curr;int color=stp[index_stp];if(fa[color]<index_fa||fa[color]==0)//this color is handled,or this color is not is fav listreturn find_color(curr,index_stp+1,index_fa,fa,stp);else if(fa[color]>index_fa){return max(find_color(curr,index_stp+1,index_fa,fa,stp),find_color(curr,index_stp+1,fa[color],fa,stp)+1);}else return find_color(curr,index_stp+1,index_fa,fa,stp)+1;}int main(){scanf("%d",&n);int *fa=new int[n+1];memset(fa,0,(n+1)*sizeof(int));scanf("%d",&m);int i,tmp;for(i=1;i<=m;i++){scanf("%d",&tmp);fa[tmp]=i;}scanf("%d",&len);int *stp=new int[len+1];int count=0;for(i=1;i<=len;i++){scanf("%d",&tmp);if(fa[tmp]!=0)stp[++count]=tmp;}len=count;int maxstp=find_color(0,1,1,fa,stp);printf("%d\n",maxstp);return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.