题目大概说,有n个颜色的宝石,可以消除是回文串的连续颜色序列,问最少要几下才能全部消除。

  • 自然想到dp[i][j]表示序列i...j全部消除的最少操作数
  • 有几种消除的方式都能通过枚举k(i<=k<j)从min(dp[i][k],dp[k+1][j])转移
  • 还有一种先消除中间的,剩余两部分组成回文串再消除,这种消除方式转移不会。。想到的时间复杂度太高。。
  • 看了tourist的代码,发现神的转移好简洁,这种方式就是从dp[i+1][j-1](c[i]=c[j])转移的
  • 应该可以这么理解,如果c[i]=c[j],而序列i+1...j-1消除到最后一步必定会剩下一个回文串!然后这个回文串就和c[i]和c[j]拼在一起,一起消除!感觉好强。。
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int c[],d[][];
int main(){
int n;
scanf("%d",&n);
for(int i=; i<n; ++i){
scanf("%d",c+i);
}
for(int i=; i<n; ++i){
d[i][i]=;
for(int j=i+; j<n; ++j){
d[i][j]=;
}
}
for(int len=; len<=n; ++len){
for(int i=; i+len-<n; ++i){
int j=i+len-;
for(int k=i; k<j; ++k){
d[i][j]=min(d[i][j],d[i][k]+d[k+][j]);
}
if(c[i]==c[j] && len==) d[i][j]=;
else if(c[i]==c[j]) d[i][j]=min(d[i][j],d[i+][j-]);
}
}
printf("%d",d[][n-]);
return ;
}

Codeforces 607B Zuma(区间DP)的更多相关文章

  1. codeforces 607B. Zuma 区间dp

    题目链接 给一个长度为n的序列, 每一次可以消去其中的一个回文串, 问最少几次才可以消完. 代码很清楚 #include <iostream> #include <vector> ...

  2. CodeForces 607B zuma

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

  3. CF607B Zuma(区间dp)

    题意 题目链接 Sol 裸的区间dp,转移的时候判一下两个字符是否相等即可 #include<bits/stdc++.h> #define Pair pair<int, int> ...

  4. Palindromic characteristics CodeForces - 835D (区间DP,预处理回文串问题)

    Palindromic characteristics of string s with length |s| is a sequence of |s|integers, where k-th num ...

  5. BZOJ 1032 JSOI 2007 祖码Zuma 区间DP

    题目大意:依照祖玛的玩法(任意选颜色),给出一段区间.问最少用多少个球可以把全部颜色块都消除. 思路:把输入数据依照连续的块处理.保存成颜色和数量.然后用这个来DP.我们知道,一个单独的块须要两个同样 ...

  6. CodeForces - 1114D-Flood Fill (区间dp)

    You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The  ...

  7. 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 ...

  8. 【CF607B】Zuma——区间dp(记忆化搜索/递推)

    以下是从中文翻译成人话的题面: 给定一个长度小于等于500的序列,每个数字代表一个颜色,每次可以消掉一个回文串,问最多消几次可以消完? (7.16) 这个题从洛谷pend回来以后显示有103个测试点( ...

  9. Codeforces 1107E(区间dp)

    用solve(l, r, prefix)代表区间l开始r结束.带了prefix个前缀str[l](即l前面的串化简完压缩成prefix-1个str[l],加上str[l]共有prefix个)的最大值. ...

随机推荐

  1. September 12th 2016 Week 38th Monday

    After all, tomorrow is another day. 不管怎样,明天又是全新的一天. Another day? Maybe.  Remember when you reach for ...

  2. SQLServer视图

    视图简介:通过定义 SELECT 语句以检索将在视图中显示的数据来创建视图.SELECT 语句引用的数据表称为视图的基表.在SQL Server 2005系统中,可以把视图分为3种类型,即标准视图,索 ...

  3. 设计模式之Singleton

    class Singleton { private Singleton() { } private static Singleton instance; // v0.1 // public stati ...

  4. 很少有人会告诉你的Android开发基本常识

    原文:很少有人会告诉你的Android开发基本常识. 文章介绍了一些关于开发.测试.版本管理.工具使用等方面的知识.

  5. MVA Universal Windows Apps系列学习笔记1

    昨天晚上看了微软的Build 2015大会第一天第一场演讲,时间还挺长,足足3个小时,不过也挺震撼的.里面提到了windows 10.Microsoft edge浏览器.Azure云平台.Office ...

  6. 安装oracle 10g RAC执行的几个脚本说明

    1,/u01/app/oracle/oraInventory/orainstRoot.sh 脚本 #!/bin/sh if [ -d "/etc" ]; then /etc; fi ...

  7. 机器学习系列:python

    工欲善其事,必先利其器!        机器学习的理论需要有编程语言才能得以实现,我选择 python 作为编程语言,网络上有篇不错的教程:python 初级教程:入门详解. 转载自http://ww ...

  8. Linux环境下stl库使用(vector)

    step1: #include <iostream> #include <vector> #include <string> using namespace std ...

  9. 利用Visual GDB在Visual Studio中进行Android开发

    转载请注明http://www.cnblogs.com/adong7639/p/4119467.html 无意中发现了Visual GDB这个工具,可以再Visual Studio中进行Android ...

  10. 创建对象为什么要 init?

    self 为什么要赋值为[super init]:”,当程序进入到init这个方法的时候,系统已经生成了对象并分配了存储空间,在调用[super init]是为了初始化父类对象,在父类对象初始化过程序 ...