HDU 6212 Zuma(区间dp)
http://acm.hdu.edu.cn/showproblem.php?pid=6212
题意:
有一行的祖玛,只由1和0组成,每次出现连续三个及以上的就会消去,问你最少需要发射多少个球才能消完。
思路:
区间最优值问题。先处理一下,把连续相同的放在一起。
对于区间$(i,j)$来说,只有3种情况:
①:把$(i,j)$分成两部分,$d[i][j]=min(d[i][j],d[i][k]+d[k+1][j])$。
②:如果i和j是相同的,那么可以把中间的处理了,然后在合并两端的,$d[i][j]=min(d[i][j],d[i+1][j-1]+(a[i]+a[j]==2))$。
③:如果i和j是相同的,并且两个不同数不同时为2的情况下,可以三个来完成合并,$d[i][j]=min(d[i][j],d[i+1][k-1]+d[k+1][j-1])$。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; char s[maxn];
int a[maxn];
int d[maxn][maxn]; int main()
{
//freopen("in.txt","r",stdin);
int T;
int kase = ;
scanf("%d",&T);
while(T--)
{
int tot=;
scanf("%s",s);
a[++tot]=;
int len=strlen(s);
for(int i=;i<len;i++)
{
if(s[i]!=s[i-]) a[++tot]=;
a[tot]++;
}
for(int r=;r<=tot;r++)
for(int i=;i+r-<=tot;i++)
{
int j=i+r-;
if(i==j) d[i][j]=-a[i];
else
{
d[i][j]=*tot;
for(int k=i;k<j;k++) d[i][j]=min(d[i][j],d[i][k]+d[k+][j]);
if((j-i)&) continue;
d[i][j]=min(d[i][j],d[i+][j-]+(a[i]+a[j]==));
if(a[i]+a[j]<)
{
for(int k=i+;k<j;k+=)
if(a[k]==) d[i][j]=min(d[i][j],d[i+][k-]+d[k+][j-]);
}
}
}
printf("Case #%d: %d\n",++kase, d[][tot]);
}
return ;
}
HDU 6212 Zuma(区间dp)的更多相关文章
- HDU 6212 Zuma 2017青岛网络赛 区间DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6212 解法:看了眼题就发现这个BZOJ 1032不是一毛一样?但是BZOJ上那是个巨坑,数据有错,原来 ...
- hdu 5396 Expression(区间dp)
Problem Description Teacher Mai has n numbers a1,a2,⋯,anand n−1 operators("+", "-&quo ...
- You Are the One HDU - 4283 (区间DP)
Problem Description The TV shows such as You Are the One has been very popular. In order to meet the ...
- CF607B Zuma(区间dp)
题意 题目链接 Sol 裸的区间dp,转移的时候判一下两个字符是否相等即可 #include<bits/stdc++.h> #define Pair pair<int, int> ...
- BZOJ 1032 JSOI 2007 祖码Zuma 区间DP
题目大意:依照祖玛的玩法(任意选颜色),给出一段区间.问最少用多少个球可以把全部颜色块都消除. 思路:把输入数据依照连续的块处理.保存成颜色和数量.然后用这个来DP.我们知道,一个单独的块须要两个同样 ...
- Dire Wolf HDU - 5115(区间dp)
Dire Wolf Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total ...
- 【CF607B】Zuma——区间dp(记忆化搜索/递推)
以下是从中文翻译成人话的题面: 给定一个长度小于等于500的序列,每个数字代表一个颜色,每次可以消掉一个回文串,问最多消几次可以消完? (7.16) 这个题从洛谷pend回来以后显示有103个测试点( ...
- HDU 5568 sequence2 区间dp+大数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5568 题意: 求所有长度为k的严格升序子序列的个数. 题解: 令dp[i][k]表示以i结尾的长度为 ...
- 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 ...
随机推荐
- [转]Hive开发经验问答式总结
本文转载自:http://www.crazyant.net/1625.html 本文是自己开发Hive经验的总结,希望对大家有所帮助,有问题请留言交流. Hive开发经验思维导图 Hive开发经验总结 ...
- Codeforces 124A - The number of positions
题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't ...
- How to diagnose vehicle fault code by BMW ICOM and ISTA-D software
Today illustrate how to diagnose vehicle fault code by BMW diagnostic tool BMW ICOM and ISTA-D softw ...
- C#简单线程
一.实例1 static void Main(string [] args) { Console.WriteLine("开始线程"); startFunc(); Console.W ...
- Python+OpenCV图像处理(五)—— 像素运算
最近在忙毕业设计,只能偶尔更新博客........ 一.像素的算术运算 像素的算术运算涉及加减乘除等基本运算(要进行算术运算,两张图片的形状(shape)必须一样) 代码如下: #像素的算术运算(加. ...
- 推荐:全新Java开发思维导图
蓦然回首自己做开发已经十年了,这十年中我获得了很多,技术能力.培训.出国.大公司的经历,还有很多很好的朋友. 但再仔细一想,这十年中我至少浪费了五年时间,这五年可以足够让自己成长为一个优秀的程序员,可 ...
- java.lang.Object.wait(Native Method)
java.lang.Object.wait(Native Method) java.lang.Object.wait(Object.java:502) java.util.TimerThread.ma ...
- 开启redis-server提示 # Creating Server TCP listening socket *:6379: bind: Address already in use--解决方法
在bin目录中开启Redis服务器,完整提示如下: 3496:C 25 Apr 00:56:48.717 # Warning: no config file specified, using the ...
- 过滤特殊字符(包括过滤emoji表情)
/** * 过滤特殊字符 * @param $text * @return mixed */ public static function filterSpecialChars($text) { // ...
- jQuery实现省市联动
未实现任何功能之前: 实现功能之后: 实现该功能的jQuery核心代码: <script> var provinceArr=new Array(5); provinceArr[0]=new ...