1045. Favorite Color Stripe (30)

Source: Internet
Author: User

LCS Maximum Common subsequence. memset function requires include <string.h>

Time limit of MS
Memory Limit 65536 KB
Code length limit 16000 B
Procedures for the award of questions StandardAuthor Chen, Yue

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

Source: >  
  
 
  1. #include<iostream>
  2. #include<stdio.h>
  3. #pragma warning(disable:4996)
  4. using namespace std;
  5. int like[201], given[10001], mp[201][10001];
  6. int main(void) {
  7. memset(mp, 0, sizeof(mp));
  8. //freopen("Text.txt", "r", stdin);
  9. int n;
  10. cin >> n;
  11. int m;
  12. cin >> m;
  13. for (int i = 1; i <= m; i++) {
  14. cin >> like[i];
  15. }
  16. int p;
  17. cin >> p;
  18. for (int i = 1; i <= p; i++) {
  19. cin >> given[i];
  20. }
  21. for (int i = 1; i <= m; i++) {
  22. Span class= "KWD" >for ( int J = 1 J <= p J ++) {
  23. Span class= "Typ" >int max = MP Span class= "pun" >[ i - 1 ][ j - 1 ;
  24. if (mp[i - 1][j]>max)
  25. max = mp[i - 1][j];
  26. if (mp[i][j - 1]>max)
  27. max = mp[i][j - 1];
  28. if (given[j] == like[i])
  29. max += 1;
  30. mp[i][j] = max;
  31. }
  32. }
  33. cout << mp[m][p];
  34. return 0;
  35. }




From for notes (Wiz)

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.