题目大意:
  祖玛游戏。
  给你一个01串,你可以往里面加一些0或1,如果连续的0或1超过3个,那么就可以消去。问消去所有的珠子至少要加几个珠子。

思路:
  区间DP。
  首先把原来的01串,改成存储连续的同种颜色的珠子有几个。
  考虑只有一种珠子时,f[i][j]=3-a[i];
  若当前区间有多种颜色的珠子,分为以下几种情况:
    1.由两个区间合并而来,f[i][j]=std::min(f[i][j],f[i][k]+f[k+1][j]);
    2.如果当前区间内有奇数个连续的同色珠子块,又分为以下2种情况:
      1).消去中间的块以后,两边的块合并:f[i][j]=std::min(f[i][j],f[i+1][j-1]+(a[i]+a[j]==2));
      2).分别消去中间两个块后,左、中、右的块合并:f[i][j]=std::min(f[i][j],f[i+1][k-1]+f[k+1][j-1])。

 #include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
char s[N];
int a[N],f[N][N];
int main() {
int T=getint();
for(register int t=;t<=T;t++) {
scanf("%s",s);
a[]=a[]=;
for(register int i=;s[i];i++) {
if(s[i]==s[i-]) {
a[a[]]++;
} else {
a[++a[]]=;
}
}
for(register int i=;i<=a[];i++) {
a[i]=std::min(a[i],);
}
for(register int j=;j<=a[];j++) {
for(register int i=j;i;i--) {
if(i==j) {
f[i][j]=-a[i];
continue;
}
f[i][j]=(j-i+)<<;
for(register int k=i;k<j;k++) {
f[i][j]=std::min(f[i][j],f[i][k]+f[k+][j]);
}
if((j-i)&) continue;
f[i][j]=std::min(f[i][j],f[i+][j-]+(a[i]+a[j]==));
if(a[i]+a[j]<=) {
for(register int k=i+;k<j-;k++) {
if(a[k]!=) continue;
f[i][j]=std::min(f[i][j],f[i+][k-]+f[k+][j-]);
}
}
}
}
printf("Case #%d: %d\n",t,f[][a[]]);
}
return ;
}

[HDU6212]Zuma的更多相关文章

  1. hdu6212 Zuma(区间dp)

    #pragma GCC optimize(2) #include <bits/stdc++.h> #define ll long long #define ls(i) i<<1 ...

  2. bzoj1032 [JSOI2007]祖码Zuma

    1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 672  Solved: 335[Submit][Stat ...

  3. Codeforces Round #336 Zuma

    D. Zuma time limit per test:  2 seconds memory limit per test:  512 megabytes input:  standard input ...

  4. Codeforces Round #336 (Div. 2) D. Zuma 区间dp

    D. Zuma   Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...

  5. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  6. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  7. BZOJ 1032 [JSOI2007]祖码Zuma

    1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 637  Solved: 318[Submit][Stat ...

  8. [LeetCode] Zuma Game 题解

    题目 题目 Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B ...

  9. [LeetCode] Zuma Game 祖玛游戏

    Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...

随机推荐

  1. Pytorch多进程最佳实践

    预备知识 模型并行( model parallelism ):即把模型拆分放到不同的设备进行训练,分布式系统中的不同机器(GPU/CPU等)负责网络模型的不同部分 —— 例如,神经网络模型的不同网络层 ...

  2. nfs挂载出错:mount.nfs: access denied by server while mounting

    这个问题就是服务器不允许客户端去挂载,那么修改服务端的权限 $ sudo vi /etc/hosts.deny 文本末添加 ### NFS DAEMONS portmap: ALL lockd: AL ...

  3. Python爬虫学习1: Requests模块的使用

    Requests函数库是学习Python爬虫必备之一, 能够帮助我们方便地爬取. Requests: 让HTTP服务人类. 本文主要参考了其官方文档. Requests具有完备的中英文文档, 能完全满 ...

  4. SHELL 中的变量

    变量的分类 系统环境变量 系统本身所有,通常为大写字母 系统变量通过 set 或 declare 指令进行查看 UDV 变量(user defined variable ) 用户创建和维护,建议大写 ...

  5. maxout激活函数

    maxout的拟合能力是非常强的,它可以拟合任意的的凸函数.最直观的解释就是任意的凸函数都可以由分段线性函数以任意精度拟合(学过高等数学应该能明白),而maxout又是取k个隐隐含层节点的最大值,这些 ...

  6. Hex Dump In Many Programming Languages

    Hex Dump In Many Programming Languages See also: ArraySumInManyProgrammingLanguages, CounterInManyPr ...

  7. CVE-2013-0025

    Microsoft IE ‘SLayoutRun’释放后重用漏洞(CNNVD-201302-197) Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WE ...

  8. SQL技巧两则:选择一个表的字段插入另一个表,根据其它表的字段更新本表内容

    最近,在作django数据表迁移时用到的. 因为在django中,我把本来一个字符型字段,更改成了外键, 于是,哦喝~~~字符型字段相当于被删除了, 为了能导入这些字段的外键信息,于是出此下策. 其实 ...

  9. HBase(十)HBase性能调优总结

    一. HBase的通用优化 1 高可用 在 HBase 中 Hmaster 负责监控 RegionServer 的生命周期,均衡 RegionServer 的负载,如果 Hmaster 挂掉了,那么整 ...

  10. centos7.x firewall简单使用

    开放一个端口: firewall-cmd --zone=public --add-port=22/tcp –permanent 开放一个范围端口对外部所有地址生效: firewall-cmd --zo ...