LightOJ 1422 Halloween Costumes (区间dp 带匹配的)

B - Halloween Costumes

 

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


区间dp的基础题,这类题目在状态转移之前要先对dp[i][j]检查是否匹配,这是因为由len更新到len+1的区间长度时,当i和j指向的数值匹配时,dp[i][j]会需要对i和j匹配的情况再做处理,而不是仅仅用s把它分割成两个小于等于len的区间进行更新就可以的。这就是石子归并基础问题(http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737)和括号匹配基础问题(http://poj.org/problem?id=2955)的差别
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int dp[110][110];
int a[110];
int main(int argc,const char* argv[])
{
    int T,cases=1;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++) //如果区间长度为0的话,初始化成1
            dp[i][i]=1;
        for(int len=1; len<n; len++)
        {
            for(int i=1,j=i+len; j<=n;i++,j++) //匹配,让第一件和最后一件共用
            {
                if(a[i]==a[j])
                    dp[i][j]=dp[i][j-1];
                else
                    dp[i][j]=dp[i][j-1]+1;
                for(int s=i;s<j;s++)
                    dp[i][j]=min(dp[i][j],dp[i][s]+dp[s+1][j]);//区间长度小于等于len-1的值都已经更新过了,所以新加入进来的只要不是和最先位配对的情况都在这个式子体现出来了
            }
        }
        cout<<"Case "<<cases++<<": "<<dp[1][n]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xuzonghao/article/details/80741744
今日推荐