https://codeforces.com/contest/1132/problem/F

思维 + 区间dp

题意

给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同时消去,问最少需要消去多少次

题解

  • 定义dp[l][r]为区间[l,r]剩下一个字符所需要的最小次数
  • dp[l][r]=min(dp[l][i]+dp[i+1][r]+x)
  • x为消去剩下两个字符所需要的次数,假如两个字符相同需要x=-1

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,f[505][505];
char s[505];
ll dfs(int l,int r){
if(l>r)return 0;
if(l==r)return 1;
ll &ans=f[l][r];
if(ans!=-1)return ans;
ans=1e17;
for(int i=l;i<r;i++){
ll tp=dfs(l,i)+dfs(i+1,r);
if(s[l]==s[i+1]||s[i]==s[i+1]||s[i]==s[r]||s[l]==s[r])
ans=min(ans,tp-1);
else ans=min(ans,tp);
}
return ans;
} int main(){
cin>>n;
scanf("%s",s+1);
memset(f,-1,sizeof(f));
cout<<dfs(1,n);
}

Educational Codeforces Round 61 F 思维 + 区间dp的更多相关文章

  1. Educational Codeforces Round 61

    Educational Codeforces Round 61 今早刚刚说我适合打pikmike出的EDU 然后我就挂了 A 不管 B 不管 C 这道题到快结束了才调出来 大概就是\(n^2\)枚举不 ...

  2. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  3. Educational Codeforces Round 61 (Rated for Div. 2) D,F题解

    D. Stressful Training 题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有n台电脑,每台电脑都有初始电量ai,也有一个 ...

  4. Educational Codeforces Round 61 (Rated for Div. 2)

    A. Regular Bracket Sequence 题意:给出四种括号的数量 ((  )) ()  )( 问是否可以组成合法的序列(只能排序不能插在另外一个的中间) 思路: 条件一:一个或 n个) ...

  5. CF1132.Educational Codeforces Round 61(简单题解)

    A .Regular Bracket Sequence 题意:给定“((” , “()” ,  “)(”,  “))”四种,问是否可以组成合法括号匹配 思路:设四种是ABCD,B可以不用管,而C在A或 ...

  6. Educational Codeforces Round 61 (Rated for Div. 2)F(区间DP,思维,枚举)

    #include<bits/stdc++.h>typedef long long ll;const int inf=0x3f3f3f3f;using namespace std;char ...

  7. Educational Codeforces Round 61 (Rated for Div. 2)D(二分,模拟,思维)

    #include<bits/stdc++.h>using namespace std;typedef long long ll;int n,k;ll a[200007],b[200007] ...

  8. Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)

    https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...

  9. Educational Codeforces Round 51 D. Bicolorings(dp)

    https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...

随机推荐

  1. Linux后台运行进程

    方式一: /bin/bash /var/www/html/crontab.sh & 缺点:终端关闭的话,进程也会关闭. 方式二: nohup /bin/bash /var/www/html/c ...

  2. mysql lost connection to server during query

    vim /etc/mysql/mysql.conf.d/mysqld.cnf [mysqld]#3600000/1000=3600秒=1小时 wait_timeout =3600000#2G缓冲max ...

  3. 关于sortedlist 中值的添加,删除,索引测试.

    SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问. 排序列表是数组和哈希表的组合.它包含一个可使用键或索引访问各项的列表.如果您使用索引访问各项,则它是一个 ...

  4. js sort

    排序算法 比较的过程必须通过函数抽象出来.通常规定,对于两个元素x和y,如果认为x < y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1,这样,排序算法就不用关 ...

  5. C# 通过api函数GetPrivateProfileString读取ini文件,取不到值

    通过api函数GetPrivateProfileString读取ini文件,取不到值,测试了好长时间,都不行 确认程序,ini文件都没有错误的情况,最后发现是ini文件编码的原因. 将ini文件的编码 ...

  6. sublime 注释模版插件DocBlockr的使用

    一.gihub地址 https://github.com/spadgos/sublime-jsdocs/ 其中有使用的教程可以参考 二.配置示例 安装教程此处略,请自行查找教程 jsdocs_extr ...

  7. iOS.XcodeUsage

    1. Customizing Xcode File Templates http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa- ...

  8. 2018 How to register and install LAUNCH ICARSCAN software ?

    2018 New Version ICARSCAN is available now! Here’s the instruction on how to install ICARSCAN softwa ...

  9. CButtonST|CUniButton等按钮类的使用

    CButtonST CButtonST类的使用参考链接:http://www.cnblogs.com/lidabo/archive/2012/12/17/2821122.html CCeButtonS ...

  10. 10个办法让设计小白迅速get海报设计要点!

    对于设计师而言,海报和宣传单的设计,几乎是每一个设计师的必修课.如今网页上的 Banner.宣传和促销的数字海报,大多脱胎于我们所熟知的海报设计. 对于推销产品.连接客户,海报始终是一种最为有趣和实用 ...