Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output

Print a single integer — the minimum number of seconds needed to destroy the entire line.

Example

Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2

题意:给定一排数字串,每次操作可以删去其中一段回文串,问至少需要多少次操作。

思路:区间DP,对长度为1和2的特殊处理:

如果为1,那么dp[i][i]=1;如果为2。

如果为2,那么dp[i][j]取决于a[i]和a[j]是否相同。

否则,先根据边界是否相同初始化,然后区间DP。

当然,也可以用普通DP实现,dp[i]=max(dpx),可以用hash验证后面是否是回文串。(感觉没问题吧。)

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=;
int dp[maxn][maxn],a[maxn];
int main()
{
int N,i,j,k;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&a[i]);
for(i=N;i>=;i--){
for(j=i;j<=N;j++){
if(j-i==) dp[i][j]=;
else if(j-i==) dp[i][j]=(a[i]==a[j])?:;
else {
if(a[i]==a[j]) dp[i][j]=dp[i+][j-];
else dp[i][j]=dp[i+][j-]+;
for(k=i;k<j;k++) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
}
printf("%d\n",dp[][N]);
return ;
}

CodeForces-607B:Zuma (基础区间DP)的更多相关文章

  1. Codeforces 607B Zuma(区间DP)

    题目大概说,有n个颜色的宝石,可以消除是回文串的连续颜色序列,问最少要几下才能全部消除. 自然想到dp[i][j]表示序列i...j全部消除的最少操作数 有几种消除的方式都能通过枚举k(i<=k ...

  2. Codeforces - 149D 不错的区间DP

    题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...

  3. CodeForces 607B zuma

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

  4. Codeforces.392E.Deleting Substrings(区间DP)

    题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...

  5. Codeforces 983B. XOR-pyramid【区间DP】

    LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...

  6. CodeForces - 1025D: Recovering BST (区间DP)

    Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...

  7. Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)

    题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...

  8. Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理

    转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...

  9. codeforces 607B. Zuma 区间dp

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

随机推荐

  1. 在dedecms系统下, 改写火车头的入库接口 写一个接口文件运行一次自动读取 http://news.163.com/rank/

    1:火车头入库接口里面的密码与login.php传过来的密码是保持一致的: 2:在(!$ispost)里面编写一个form表单提交,验证用户名,channelid,以及typeid; html代码格式 ...

  2. 王垠:完全用Linux工作 (2003)

    完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...

  3. VirtualBox虚拟机出现被召者 RC: E_NOINTERFACE (0x80004002)

    选中VirtualBox右键 就可以正常运行了

  4. 高性能JS-DOM

    用脚本进行DOM操作的代价是很昂贵的,它是富web应用中最常见的性能瓶颈.主要有以下三种问题: 访问和修改DOM元素 修改DOM元素的样式导致repaint和reflow 通过DOM事件处理与用户进行 ...

  5. 【原】理解javascript中的闭包(***********************************************)

    阅读目录 什么是闭包? 闭包的特性 闭包的作用: 闭包的代码示例 注意事项 总结 闭包在javascript来说是比较重要的概念,平时工作中也是用的比较多的一项技术.下来对其进行一个小小的总结 回到顶 ...

  6. VC/MFC中计算程序运行时间

    转自原文VC/MFC中计算程序运行时间 说明,这四种方法也分别代表了类似的实现,在MFC中,所可以从哪些类集合去考虑. 方法一 利用GetTickCount函数(ms) CString str; lo ...

  7. 阿里云OSS 图片处理api(custom)

    阿里云OSS 图片处理api(custom) 阿里云对象存储服务(Object Storage Service, 简称OSS) 学习了:https://blog.csdn.net/u014559227 ...

  8. python 学习笔记 13 -- 经常使用的时间模块之time

    Python 没有包括相应日期和时间的内置类型.只是提供了3个相应的模块,能够採用多种表示管理日期和时间值: *    time 模块由底层C库提供与时间相关的函数.它包括一些函数用于获取时钟时间和处 ...

  9. 【转载】关于Hash

    这个HASH算法不是大学里数据结构课里那个HASH表的算法.这里的HASH算法是密码学的基础,比较常用的有MD5和SHA,最重要的两条性质,就是不可逆和无冲突.所谓不可逆,就是当你知道x的HASH值, ...

  10. SpringMVC框架下使用jfreechart绘制折线图,柱状图,饼状图

    java代码 @Controller public class CityAction { @Autowired private CityBiz cityBiz; //柱状图 @RequestMappi ...