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. [LeetCode_96] Unique Binary Search Trees

    题目链接 https://leetcode.com/problems/unique-binary-search-trees/ 题意 计算给定节点数的BST有多少种 思路 递归 相关知识 二叉搜索树(B ...

  2. sqlserver判断字段是否存在更改字段

    use naire go if COL_LENGTH('options','optionsGroup') is null begin--options为表名,optionsGroup为列名 alter ...

  3. 整合使用持久层框架mybatis 使用SqlSessionTemplate模板类与使用映射接口 对比

    spring中整合使用mybatis的用法总结 一:在Spring配置Mybatis 第一步:将mybatis-spring类包添加 到项目的类库中 第二步:编写spring和持久层衔接的xml文件, ...

  4. mysql中各种join连表查询总结

    通常我们需要连接多个表查询数据,以获取想要的结果. 一.连接可以分为三类: (1) 内连接:join,inner join (2) 外连接:left join,left outer join,righ ...

  5. Oracle高级查询之OVER

    注释:为了方便大家学习和测试,所有的例子都是在Oracle自带用户Scott下建立的 oracel的高级用法:rank()/dense_rank() over(partition by ...orde ...

  6. 获取URL某个参数

    /* 获取URL某个参数(可以是中文) * 返回:字符串 */ function getUrlParam(key) { // 获取参数 var url = window.location.search ...

  7. Windows下PythonQt编译(vs2015+Qt5.11.2+PythonQt 3.2)

    后记: 由于自己low,没有下载罪行的python3.2导致编译上遇到种种问题,后文可以参考,建议看: <Windows7 VS2015 下编译 PythonQt3.2> https:// ...

  8. Linux ulimit

    一.简介   二.语法   三.其他 1)linux下进程的进程最大数.最大线程数.进程打开的文件数和ulimit命令修改硬件资源限制 http://blog.csdn.net/gatieme/art ...

  9. C# iframe session 丢失

    在页面A中使用iframe引用另一站点页面B,但页面B上面的session总是丢失,百度了一下,不用改程序,直接在iis里面操作,解决方法如下 1.打开IIS管理器 inetmgr 2.选择被嵌入if ...

  10. 优化myeclipse启动速度以及解决内存不足问题

    解决myeclipse内存不足问题: 使用 MyEclipse 开发项目后,随着项目文件的增多,以及运行时间的增加,实际上 MyEclipse 所消耗的内存是会一直增大的,有的时候会出现 MyEcli ...