题意:一个字符串被定义为“alphabetical”,当且仅当它能够删除一些字符,变成“ab...yz”,给你一个只由小写字母组成的字符串,问你最少插入几个字母,使它变成“alphabetical”的。

f(i,j)表示前i个字母,以j为结尾时,最少要插入几个字母。

f(i,j)=min{f(i-1,k)+j-k+(s[i]>k && s[i]<=j) ('a'-1<=k<j) , f(i-1,j)}。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[55][205];
char a[55];
int n;
int main(){
// freopen("a.in","r",stdin);
scanf("%s",a+1);
n=strlen(a+1);
memset(f,0x7f,sizeof(f));
for(int i='a';i<='z';++i){
f[0][i]=i-'a'+1;
}
for(int i=0;i<=n;++i){
f[i]['a'-1]=0;
}
for(int i=1;i<=n;++i){
for(int j='a';j<='z';++j){
for(int k='a'-1;k<j;++k){
f[i][j]=min(f[i][j],f[i-1][k]+j-k-(a[i]>k && a[i]<=j));
}
f[i][j]=min(f[i][j],f[i-1][j]);
// printf("%d ",f[i][j]);
}
// puts("");
}
printf("%d\n",f[n]['z']);
return 0;
}

【动态规划】Gym - 101201A - Alphabet的更多相关文章

  1. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  2. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  3. Gym 100829S_surf 动态规划的优化

    题目大意是,非你若干个任务,任务分别对应开始时间.预期收益.持续时间三项指标,让你从中选择一个受益最大的方案(没有开始时间相同的任务). 于是,标准状态转移方程应当为,设DP[K]为选择了前K个任务的 ...

  4. 【动态规划】Gym - 101102A - Coins

    Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of ...

  5. Gym - 101982C Contest Setting (动态规划)

    A group of contest writers have written n problems and want to use k of them in an upcoming contest. ...

  6. 【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences

    求最长上升子序列方案数. 转载自:http://blog.csdn.net/u013445530/article/details/47958617,如造成不便,请博主联系我. 数组A包含N个整数(可能 ...

  7. 【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs' Box Boutique

    给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排 ...

  8. 【动态规划】Gym - 100507G - The Debut Album

    一般思路的dp是用f(i,j,0)表示前i位最后有j个1的方案数,用f(i,j,1)表示前j位最后有j个2的方案数,j都是大于等于1的,然后比较容易转移. 但这题卡内存,就只能用f(i,j)表示前i位 ...

  9. 【动态规划】Gym - 101147H - Commandos

    裸dp,看代码. #include<cstdio> #include<algorithm> #include<cstring> using namespace st ...

随机推荐

  1. CSUST选拔赛题解

    本鶸鸡于本月10号参加了蔽校的选拔赛,成绩差的死,大部分的题都是赛后花了好长时间才补出来的,其中有些题还是靠QAQorz大佬帮忙才能解决,感谢Qls对我的帮助~接下来就附带上我的暴力题解,大佬们有更好 ...

  2. Html5_sessionStrong和localStorage的灵活使用

    谈谈这两个属性sessionStrong和localStorage是Html5新增点属性,用来记录一些数据在浏览器. 两者的区别sessionStrong存储的数据是暂时的,浏览器关掉后,存储下来的数 ...

  3. 我的spring boot,杨帆、起航!

    快速新建一个spring boot工程可以去http://start.spring.io/这个网址,配置完后会自动下载一个工程的压缩包,解压后导入相关ide工具即可使用. 工程中会自带一个class启 ...

  4. aircrack加reaver破解带有wps的wifi

    最近心血来潮,想把小区里的无线信号测试个遍.基于目前大多数路由器都支持wps,想必各位基友们都知道aircrack和reaver这 两个工具,实属破解pin码,杀人越货,居家旅行之必备良药.像以前跑r ...

  5. 修改ES使用root用户运行

    默认ES不允许使用root用户运行,如果使用root会报如下图的错误: ,通常建议创建elsearch用户并使用该用户运行ES.但如果必须使用root用户时,按如下设置即可: 1.启动是使用如下命令 ...

  6. jQuery为多个元素绑定同一个事件

    $('.toals,input[type=datetime]').on('focus',function(){ $('.footer-focus-none').css('display','none' ...

  7. Leetcode 之Evaluate Reverse Polish Notation(41)

    很简单的一道题,定义一个栈保留操作数,遇操作符则弹出运算即可. bool isOperator(string &op) { //注意用法 && string("+-* ...

  8. window,getComputedStyle,letter-spacing

       js 拿到element的css样式    window.getComputedStyle(ele,[pseuso)    比如想拿到一个element的背景色 window.getComput ...

  9. LeetCode解题报告—— Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  10. LeetCode解题报告—— Regular Expression Matching

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...