[HDU6212]Zuma
题目大意:
祖玛游戏。
给你一个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的更多相关文章
- hdu6212 Zuma(区间dp)
#pragma GCC optimize(2) #include <bits/stdc++.h> #define ll long long #define ls(i) i<<1 ...
- bzoj1032 [JSOI2007]祖码Zuma
1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 672 Solved: 335[Submit][Stat ...
- Codeforces Round #336 Zuma
D. Zuma time limit per test: 2 seconds memory limit per test: 512 megabytes input: standard input ...
- 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 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Codeforces Round #336 (Div. 2) D. Zuma
Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...
- BZOJ 1032 [JSOI2007]祖码Zuma
1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 637 Solved: 318[Submit][Stat ...
- [LeetCode] Zuma Game 题解
题目 题目 Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B ...
- [LeetCode] Zuma Game 祖玛游戏
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...
随机推荐
- 【连接查询】mySql多表连接查询与union与union all用法
1.准备两个表 表a: 结构: mysql> desc a; +-------+-------------+------+-----+---------+-------+ | Field | T ...
- Shell-脚本只能运行1次
用空文件进行判断 path=`pwd` if [ -f ${path}/.runned ]; then { echo "This script can only execute once! ...
- Nodejs 发送邮件
var nodemailer = require("nodemailer");var mailTitle='http://bemupa.forumieren.com:Best Mu ...
- Django模型和ORM
一.ORM ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是 ...
- 二十二、springboot之监控管理Actuator
1.新增spring-boot-starter-actuator的依赖 gradle: compile('org.springframework.boot:spring-boot-actuator') ...
- Python_oldboy_自动化运维之路(四)
本节内容 集合 字符编码与转码 函数语法及基本特性 函数参数与局部变量 返回值和嵌套函数 递归 匿名函数 高阶函数 1.集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变 ...
- Nginx - upstream 模块及参数测试
目录 - 1. 前言- 2. 配置示例及指令说明 - 2.1 配置示例 - 2.2 指令 - 2.3 upstream相关变量- 3. 参数配置及测试 - 3.1 max_fa ...
- caffe卷积操作
- java基础39 增强for循环(也叫foreach循环)
增强for循环是jdk1.5出现的新功能 1.增强for循环的作用 简化了迭代器的书写格式(注意:增强for循环底层还是使用了迭代器遍历) 2.增强for循环的格式 for(数据类型 变量名:遍历的目 ...
- python随笔(三)
在对字符串的操作中,s[::-1]表示将字符串逆序输出. 字符串本身不能改变(管理者而非所有者) 列表的内容是可以改变的,且列表的内容可以不仅仅是字符串.对于一个列表,注意b=a和b=a[:]的区别. ...