PAT 1045. Favorite Color Stripe

Source: Internet
Author: User

Eva is trying-make she own color stripe out of a given one. She would like to keep only She 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 this a normal human eye can distinguish on less than-different colors, so Eva's favorite colors is Limi Ted. However the original stripe could is very long, and Eva would like to has the remaining favorite stripe with the maximum Length. So she needs your help to find her the best result.

Note that the solution might is not a unique, but is only has 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 is given in her favorite order as {2 3 1 5 6}, then she had 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 the one test case. For each case, the first line contains a positive integer N (<=200) which are the total number of colors involved (and H ence The colors is 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 fav Orite order. Finally the third line starts with a positive integer L (<=10000) which are the length of the given stripe, followed by L colors on the stripe. All the numbers in a line is 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:

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

Sample Output:

7

LCS problem variant, DP resolution

#include <cstdio>using namespacestd;intMain () {intm,l,m[202],l,dp[202]; scanf ("%*d%d",&m); dp[0] =0;  for(intI=0; i<m; i++) dp[i+1] =0, scanf ("%d", m+i); scanf ("%d",&l);  while(l--) {scanf ("%d",&m);  for(intI=0; i<m; i++)            if(L = = M[i]) dp[i+1]++; Elsedp[i+1] = Dp[i] > dp[i+1] ? Dp[i]: dp[i+1]; } printf ("%d\n", Dp[m]); return 0;}

PAT 1045. Favorite Color Stripe

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.