D. Zuma
 

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.

Sample test(s)
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
Note

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题意:给你一个串,每次可以消除 其一个回文子串,问你最少消除几次

题解:设dp[l][r] 为 l到r 需要消除几次  ,

搜索就好

//meek///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll; const int N = ;
const int inf = ;
const int mod= ; int dp[N][N],a[N],n;
int dfs(int l,int r) {
if(dp[l][r] != -) return dp[l][r];
int& ret=dp[l][r]=inf;
if(l==r) ret=;
else if(r-l == ) {
if(a[l]==a[r]) ret = ;
else ret = ;
}
else {
if(a[l] == a[r]) ret = dfs(l+,r-);
for(int i=l;i<r;i++) ret = min(ret,dfs(l,i)+dfs(i+,r));
}
return ret;
}
int main() {
scanf("%d",&n);
memset(dp,-,sizeof(dp));
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
}
printf("%d\n",dfs(,n));
return ;
}

代码

Codeforces Round #336 (Div. 2) D. Zuma 区间dp的更多相关文章

  1. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  2. Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp

    B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. Codeforces Round #336 (Div. 2) D. Zuma(区间DP)

    题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...

  4. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  5. Codeforces Round #336 (Div. 2)C. Chain Reaction DP

    C. Chain Reaction   There are n beacons located at distinct positions on a number line. The i-th bea ...

  6. Codeforces Round #538 (Div. 2)D(区间DP,思维)

    #include<bits/stdc++.h>using namespace std;int a[5007];int dp[5007][5007];int main(){    int n ...

  7. Codeforces Round #336 (Div. 2) 608C Chain Reaction(dp)

    C. Chain Reaction time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  9. Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】

    A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...

随机推荐

  1. hdu 5412 CRB and Queries

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5412 CRB and Queries Description There are $N$ boys i ...

  2. hdu 1023 Train Problem II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...

  3. [CentOS 6.5 X64]讓firefox java plugin 啟動

    到ORACLE下載JRE http://www.oracle.com/technetwork/java/javase/downloads/index.html 我是X64所以下載 jre-7-linu ...

  4. exception -----> Typedefs & Classes

    #include <exception> Typedefs exception_ptr 一种类型,描述了一个指向异常的指针 terminate_handler 一种类型,描述了一个适合作为 ...

  5. hmmer 使用(转载)

    hmmer 使用 » 转载文章请注明,转载自:博耘生物 » <hmmer的安装与使用> » 原文链接:http://boyun.sh.cn/bio/?p=1753   从功能基因研究的角度 ...

  6. linux 简单的DMA例程

    一个简单的使用DMA 例子 示例:下面是一个简单的使用DMA进行传输的驱动程序,它是一个假想的设备,只列出DMA相关的部分来说明驱动程序中如何使用DMA的. 函数dad_transfer是设置DMA对 ...

  7. 在package.json中配置Script执行npm run tslint报错问题

    今天在学习tslint的时候,按照git clone下angular2-webpack-starter的代码执行npm run lint时,虽然代码进行了检测,但检测完成后npm始终报错, //pac ...

  8. 软件工程实践小队Scrum Meeting

    例会记录 时间:2013年10月20日星期日 Part 1 会议要点: 1. 小组讨论PM.Dev.Test的各自特点: 2. 小组讨论我们的项目网上教学问答系统的相关问题: 3.确定小组成员在第一轮 ...

  9. BICEP单元测试计划-四则运算-测试

    一.6个值得测试的具体部位,他们能够提高你的测试技巧 Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制 ...

  10. Asp.net操作Excel(终极方法NPOI)(转)

    原文:Asp.net操作Excel(终极方法NPOI) 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中 ...