题目大概说,有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. Cityengine, 3ds MAX, FME

    Cityengine 和 3ds MAX 一次只可以导入 (import) 一个模型. FME可以一次导入多个模型,因此可以用它来进行数据整合,然后放到cityengine里头去现实.FZViewer ...

  2. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...

  3. hdu5878(枚举,打表)

    题目链接:hdu5878 题意:到一行输入t,表示下面有t组数据,然后下面t行每行输入一个数n; 定义x==2^a*3^b*5^c*7^d(a, b, c, d为自然数,x不大于1e+9): 要求对于 ...

  4. C#的正则表达式

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  5. .net学习之Attribute特性和EF关键知识点

    一.Attribute特性/标签1.Attribute用来对类.属性.方法等标注额外的信息,贴一个标签简单的说,定制特性Attribute,本质上就是一个类,它为目标元素提供关联附加信息,并在运行时以 ...

  6. 在ubuntu上搭建开发环境1---在windows7的基础上在安装ubuntu(双系统)

    转载:http://jingyan.baidu.com/article/60ccbceb18624464cab197ea.html 当需要频繁使用ubuntu时,vmware虚拟机下运行ubuntu, ...

  7. 网站性能测试工具--MS Web Application Stress Tool

    MS Web Applicaion Stress Tool 是一款网页测试的性能工具,具体的使用可以参考下面这篇博客文章 http://cuisuqiang.iteye.com/blog/193640 ...

  8. 【翻译十九】-java之执行器

    Executors In all of the previous examples, there's a close connection between the task being done by ...

  9. 【JAVA网络流之URL】

    一.URL URL对象可以认为是URLConnection对象+Socket对象. Java.lang.Object |-Java.net.URL 常用构造方法: URL(String spec)   ...

  10. MYSQL建表语法(主键,外键,联合主键)

    在看<Learning SQL>第二版, 慢慢打实SQL的基础. 建表一: ), lname ), gender ENUM(), city ), state ), country ), p ...