题目链接:https://codeforces.com/contest/608/problem/D

题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最少需要多少次操作可以消除所有的宝石。(每次操作消除一个回文串,最少操作次数清楚字符串)

题解:dp[l][r]表示区间(l,r)的最少操作次数,对于一个区间(l,r),有两种转移:①若存在c_l = c_i(l <= i <= r),可以由dp[l][i] + dp[i + 1][r]转移;②若c_l = c_r,可以由dp[l + 1][r - 1]转移。

 #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pi acos(-1)
#define pii pair<int,int>
#define pb push_back
#define lowbit(x) ((x)&(-x))
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = + ;
const int maxm = 1e6 + ;
const ll mod = ; int c[maxn];
int dp[maxn][maxn]; int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int n;
scanf("%d",&n);
for(int i = ; i < n; i++) {
scanf("%d",&c[i]);
dp[i][i] = ;
}
for(int i = ; i < n - ; i++) {
if(c[i] == c[i + ]) dp[i][i + ] = ;
else dp[i][i + ] = ;
}
for(int i = ; i < n; i++) {
for(int j = ; i + j < n; j++) {
dp[j][j + i] = i + ;
for(int k = j; k < j + i; k++) {
if(c[j] == c[k]) dp[j][j + i] = min(dp[j][j + i], dp[j][k] + dp[k + ][j + i]);
}
if(c[j] == c[j + i]) dp[j][j + i] = min(dp[j][j + i], dp[j + ][j + i - ]);
}
}
printf("%d\n",dp[][n - ]);
return ;
}

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

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

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

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

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

  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. hdoj1247(字典树)

    题目链接:https://vjudge.net/problem/HDU-1247 题意:给定n个字符串(n<=50000),判断其中哪些字符串恰能由另外两个不同的字符串连接而成. 思路: 暴力字 ...

  2. Quartz.Net—DateBuilder

    定时框架中最重要的就是时间,我们也可以直接使用Cron这种事件格式.  使用其他的时间格式,就可以用DateBuilder快速的创建出需要的时间. 因为quartz是一个定时框架,所以对于操控时间  ...

  3. todo---callback

    todo---callback https://blog.csdn.net/u010158267/article/details/51426963/

  4. Ctex ERROR Reading

    提供一个"Ctex ERROR Reading"的处理办法 方法/步骤: 1. 再我们打开一些网上下载的文档的时候可能出现错误提示 2. 这种问题一般是文件编码的问题,我们需要选择 ...

  5. 【MIT 6.824 】分布式系统 课程笔记(二)Lecture 03 : GFS

    Lecture 03 : GFS 一.一致性 1, 弱一致性 可能会读到旧数据 2, 强一致性 读到的数据都是最新的 3, 一致性比较 强一致性对于app的写方便, 但是性能差 弱一致性有良好的性能, ...

  6. AtomicIntegerFieldUpdater和AtomicInteger

    为什么有了AtomicInteger还需要AtomicIntegerFieldUpdater? 当需要进行原子限定的属性所属的类会被创建大量的实例对象, 如果用AtomicInteger, 每个实例里 ...

  7. 在论坛中出现的比较难的sql问题:21(递归问题 检索某个节点下所有叶子节点)

    原文:在论坛中出现的比较难的sql问题:21(递归问题 检索某个节点下所有叶子节点) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. ...

  8. js 遍历树的层级关系的实现

    1.遍历树的层级关系 1)先整理数据 2)找到id和数据的映射关系 3)然后找到父节点的数据,进行存储 test() { const list = [ { id: ", parentId: ...

  9. jQuery 基础知识

    一.序言 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后的又一个优秀的JavaScript代码库(JavaScript框架).jQuery设计的宗旨是"W ...

  10. 关于Vue-elementUI中,给input手动赋值之后无法修改的问题解决

    方案一:在data中给input的值赋一个初始值 方案二:在给input赋值时,使用this.$set