题目大意:
  祖玛游戏。
  给你一个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. 2016.5.57—— Remove Duplicates from Sorted List

    Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...

  2. C#抓取网络图片保存到本地

    C#抓取网络图片保存到本地 System.Net.WebClient myWebClient = new System.Net.WebClient(); //将头像保存到服务器 string virP ...

  3. 01 Go 1.1 Release Notes

    Go 1.1 Release Notes Introduction to Go 1.1 Changes to the language Integer division by zero Surroga ...

  4. C# 托管资源 与 非托管资源

    C# 托管资源 与 非托管资源 托管资源一般是指被CLR控制的内存资源,这些资源的管理可以由CLR来控制,.NET可以自动进行回收,主要是指托管堆上分配的内存资源.例如程序中分配的对象,作用域内的变量 ...

  5. Docker - CentOS安装Docker

    如果要在CentOS下安装Docker容器,必须是CentOS 7 (64-bit).CentOS 6.5 (64-bit) 或更高的版本,并要求 CentOS 系统内核高于 3.10. uname ...

  6. grail开发环境的搭建

    本文参考:Grails入门指南(第二版) 1. 下载jdk和Grail http://www.oracle.com/technetwork/java/javase/downloads/ http:// ...

  7. hdu 5131 (2014广州现场赛 E题)

    题意:对给出的好汉按杀敌数从大到小排序,若相等,按字典序排.M个询问,询问名字输出对应的主排名和次排名.(排序之后)主排名是在该名字前比他杀敌数多的人的个数加1,次排名是该名字前和他杀敌数相等的人的个 ...

  8. html-图片热点和网页划区

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. mycat 配置文件详解

    server.xml 包含mycat的系统配置信息,它有两个标签,分别是user和system,掌握system标签的各项配置属性是mycat调优的关键. <?xml version=" ...

  10. Python类和人类

    Python中的类 俗话说,物以类聚,人以群分,类是什么,类就是一组相同属性的集合.下面来结合人,探讨Python中类和人类的关系. 首先,我们定义一个人的类,如下: class People(obj ...