题目链接: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. linux中matlab的中文乱码问题

    ref:http://hi.baidu.com/36470902/item/47f3b133c419d989f5e4ade1 将系统的jre拷贝到matlab去 . sudo mv $MATLAB/s ...

  2. mogodb数据库简单的权限分配

    mongdb数据库默认不需要权限认证,但为了安全起见,最好设置下需要权限认证,启动的时候设置auth=true即可. 推荐通过mongodb.conf配置文件的形式启动mongodb   进入mong ...

  3. [转帖]MySQL的又一神器-锁,MySQL面试必备

    MySQL的又一神器-锁,MySQL面试必备 https://segmentfault.com/a/1190000020762791 lock 低一级的是 latch   原文链接:blog.ouya ...

  4. [VS] - Visual Studio 智能感知无法启用 之解决

    背景 VS 2017 智能感知无法使用,重置 "导入和导出设置..." 后仍无法使用. 解决 我在 VS 上安装了 Resharper 的,猜测可能跟其配置有关,重置 Intell ...

  5. springboot之Redis

    1.springboot之Redis配置 在学习springboot配置Redis之前先了解Redis. 1.了解Redis Redis简介: redis是一个key-value存储系统.和Memca ...

  6. java面向函数编程简单应用

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...

  7. Python-20-异常处理

    一.什么是异常 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止) 常用异常: AttributeError 试图访问一个对 ...

  8. 1144: 零起点学算法51——数组中删数(C语言)

    题目: 题目来源WUSTOJ 源代码: #include<stdio.h> int main() { int n, m, i, a[20]; while (scanf("%d&q ...

  9. 处理ajax数据;数据渲染(细节)

    AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通 ...

  10. Docker 方式部署的应用的版本更新

    前言 公司使用 Docker-Compose 的方式部署 Jenkins/Gitlab/Sonar/Confluence/Apollo/Harbor/ELK/MySQL 等一系列开发工具/数据库. 而 ...