码迷,mamicode.com
首页 > 其他好文 > 详细

1045. Favorite Color Stripe (30)

时间:2015-12-06 11:18:32      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

LCS 最大公共子序列。 memset函数需要include <string.h>
技术分享

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=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 are 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

 
  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. for (int j = 1; j <= p; j++) {
  23. int max = mp[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. }

 




1045. Favorite Color Stripe (30)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!