[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 ...
随机推荐
- Python——脚本(calculator)
<Python基础教程>(第二版) P123 书中原代码如下: class Calculator: def calculator(self,expression): self.value ...
- Python标准库笔记(9) — functools模块
functools 作用于函数的函数 functools 模块提供用于调整或扩展函数和其他可调用对象的工具,而无需完全重写它们. 装饰器 partial 类是 functools 模块提供的主要工具, ...
- git fetch 命令
git fetch命令用于从另一个存储库下载对象和引用. 使用语法 git fetch [<options>] [<repository> [<refspec>…] ...
- C#连接MySQL 操作步骤
1.工具安装: 安装 MySQL For Windows,这个不多说,上官网下载: 安装mysql-connector-net,这个是MySQL数据库.NET开发驱动,因为C#是.NET架构的,所以需 ...
- sqlserver中查询存储过程中的字符串
select name from sysobjects o, syscomments s where o.id = s.id and text like '%querytext%' and o.xty ...
- 使用os模块实现展示目录下的文件和文件夹
Windows 10家庭中文版,Python 3.6.4 今天学习了os模块,下面是使用它开发的一个展示目录下的文件和文件夹的函数,代码如下: import os # deep大于等于1的整数,默认为 ...
- java基础77 Http协议及Servlet中的GET、POST提交方式
本文知识点(目录): 1.什么是http协议 2.查看http协议的工具 3.http协议的内容 4.请求方式 5.请求头和响应头(以及获取请求头信息的方法) 6.实体内 ...
- Oracle 函数 “数据控制,指定某些人只能查看他权限范围内的信息”
create or replace function work_plan_mask (p_schema VARCHAR2,p_table VARCHAR2) return Varchar2 AS -- ...
- 适合新手的web开发环境
学习web开发,环境搭建是必不可少的一个环节.你可以使用wamp一键安装包,或者使用sae.bae.gae这种PaaS平台来部署,或者安装*nix系统在本地部署. 对于一个希望体验LAMP式建站的新手 ...
- 【AtCoder】AGC023 A-F题解
可以说是第一场AGC了,做了三道题之后还有30min,杠了一下D题发现杠不出来,三题滚粗了 rating起步1300+,感觉还是很菜... 只有三题水平显然以后还会疯狂--啊(CF的惨痛经历) 改题的 ...