A
来源:互联网 发布:php字符串替换 全部 编辑:程序博客网 时间:2024/06/06 00:42
Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it’s Halloween, these parties are all costume parties, Gappu always selects his costumes in such a way that it blends with his friends, that is, when he is attending the party, arranged by his comic-book-fan friends, he will go with the costume of Superman, but when the party is arranged contest-buddies, he would go with the costume of ‘Chinese Postman’.
Since he is going to attend a number of parties on the Halloween night, and wear costumes accordingly, he will be changing his costumes a number of times. So, to make things a little easier, he may put on costumes one over another (that is he may wear the uniform for the postman, over the superman costume). Before each party he can take off some of the costumes, or wear a new one. That is, if he is wearing the Postman uniform over the Superman costume, and wants to go to a party in Superman costume, he can take off the Postman uniform, or he can wear a new Superman uniform. But, keep in mind that, Gappu doesn’t like to wear dresses without cleaning them first, so, after taking off the Postman uniform, he cannot use that again in the Halloween night, if he needs the Postman costume again, he will have to use a new one. He can take off any number of costumes, and if he takes off k of the costumes, that will be the last k ones (e.g. if he wears costume A before costume B, to take off A, first he has to remove B).
Given the parties and the costumes, find the minimum number of costumes Gappu will need in the Halloween night.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing an integer N (1 ≤ N ≤ 100) denoting the number of parties. Next line contains N integers, where the ith integer ci (1 ≤ ci ≤ 100) denotes the costume he will be wearing in party i. He will attend party 1 first, then party 2, and so on.
Output
For each case, print the case number and the minimum number of required costumes.
Sample Input
2
4
1 2 1 2
7
1 2 1 1 3 2 1
Sample Output
Case 1: 3
Case 2: 4
三层暴力解决:
外层跑区间长度,最内层跑重复衣服
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;#define LL long long int#define Irish_Moonshine mainint T, N;int a[205];int dp[205][205];int Irish_Moonshine(){ scanf("%d", &T); for (int k = 1; k <= T; k++) { //cout << "250" << endl; scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d", &a[i]); dp[i][i] = 1; } for (int j = 2; j <= N; j++)//处理区间的位置 { for (int i = 1; i < j; i++)//与下一种相同,因为无差异! { dp[i][j] = dp[i][j - 1] + 1; int tmp; for (int tmp = i; tmp < j; tmp++)//是否与之前有相同的值 { if (a[tmp] == a[j]) dp[i][j] = min(dp[i][j], dp[i][tmp] + dp[tmp + 1][j - 1]); } } } printf("Case %d: %d\n", k, dp[1][N]); } //system("pause"); return 0;}
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;#define LL long long int#define Irish_Moonshine mainint T, N;int a[205];int dp[205][205];int Irish_Moonshine(){ scanf("%d", &T); for (int k = 1; k <= T; k++) { //cout << "250" << endl; scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d", &a[i]); dp[i][i] = 1; } for (int j = 2; j <= N; j++)//正推 { for (int i = j - 1; i >= 1; i--)//缩小区间 { dp[i][j] = dp[i][j - 1] + 1; int tmp; for (int tmp = i; tmp < j; tmp++)//是否与之前有相同的值 { if (a[tmp] == a[j]) dp[i][j] = min(dp[i][j], dp[i][tmp] + dp[tmp + 1][j - 1]); } } } printf("Case %d: %d\n", k, dp[1][N]); } system("pause"); return 0;}
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- A
- A*
- a
- A
- a
- 浅谈Cookie
- servlet获取HttpServletRequest、HttpServletResponse
- Android兼容包Support v4.v7.v13区别与应用场景
- 在CentOS7中利用yum命令安装mysql
- Java Web学习总结(41)——Java EE 8 新功能展望
- A
- React Native 利用自定义button封装购物车计数器
- springboot怎么使用maven打包时将本地jar包一块打进去
- 用Jmeter搭建HTTP接口自动化引擎 1.0 版本
- 明星加持,资本涌入,爆火的教育行业随随便便年薪百万?
- UVA 1515 Pool construction
- AI大事件 | 人类理解行为数据集推出,Uber发布自家分布式深度学习框架
- 用公开语料推进NLP研究,孵化现象级产品 | 专访阿里AI Labs聂再清
- Java并发编程-同步辅助类之Phaser