Zuma HDU - 6212

Think about the Zuma Game. You have a row of at most 200

black(0) or white(1) balls on the table at the start. Each three consecutive balls never share the same colour. You also have infinite amount of black and white balls in your hand. On each turn, you can choose a ball in your hand and insert it into the row, including the leftmost place and the rightmost place. Then, if there is a group of three of more balls in the same colour touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table.

Input

The first line of input contains an integer T (1≤T≤100)

which is the total number of test cases.
Each test case contains a line with a non-empty string of 0 and 1

describing the row of balls at the start.

Output

For each test case, output the case number and the minimal balls required to insert in a line.

Sample Input

4
10101
101001001
1001001001
01001101011001100

Sample Output

Case #1: 4
Case #2: 3
Case #3: 3
Case #4: 2

转载

区间dp; 考虑3中情况,

1. 枚举中间的转折点  ×××× ×××

2.如果两边相同,我们可以考虑中间的消去后,我们再去消掉两边。  ??******?

3.还有一种可能就是,两边和中间  比如  ??***?*****?.  但是要满足,两边的总和不能超过 3,因为超过3,意味着,一定会

在中间消掉之后,一定是有一个人直接消失了,导致不符合这种情况。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)

const int N=210;
char s[N];
int a[N],dp[N][N];
int main(){
    int T;
    scanf("%d",&T);
    rep(kase,0,T){
        scanf("%s",s);
        int len=strlen(s),cnt=1;
        fill(a,a+len+1,0);
        rep(i,0,len){
            if(s[i]==s[i+1])a[cnt]++;
            else{
                a[cnt++]++;
            }
        }
        --cnt;
       // rep(i,1,cnt+1) printf("i:%d %d\n",i,a[i]);
        for(int i=cnt;i>=1;--i){
            for(int j=i;j<=cnt;++j){
                if(i==j){
                    dp[i][j]=3-a[i];
                    continue;
                }
                dp[i][j]=2*(j-i+1);
                for(int k=i;k<j;++k)  dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
                if((j-i)&1)continue;

                dp[i][j]=min(dp[i][j],dp[i+1][j-1]+(a[i]+a[j]==2));

                if(a[i]+a[j]<=3){
                    for(int k=i+2;k<j;k+=2){
                        if(a[k]!=1)continue;
                        dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k+1][j-1]);
                    }
                }
            }
        }
        printf("Case #%d: %d\n",kase+1,dp[1][cnt]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值