1479: Treasure Chest Lock

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 7  Solved: 5

[

id=1479">Submit][

id=1479">Status][Web
Board
]

Description

Vic has a treasure chest. And there is a lock on the treasure chest. The lock contains a sequence of wheels. Each wheel has the 26 letters of the English alphabet

‘a’ through ‘z’, in order. If you move a wheel up, the letter it shows changes to the next letter in the English alphabet (if it was showing the last letter ‘z’, then it

changes to ‘a’). If you move the wheel down, it changes to show the previous letter in the English alphabet (if it was showing ‘a’, then it changes to ‘z’).

It is also possible to move any subsequence of contiguous wheels in the same direction with only one movement. This has the same effect of moving each of the

wheels within the subsequence on that direction, but saves the effort of doing that one wheel at a time.The lock openswhen the wheels show a secret sequence of l

etters. Currently all wheels are showing the letter ‘a’. Vic wants to know the minimum number of movements you need to open the lock.

Input

The input has several test cases. Each of them is given in exactly one line containing a nonempty string of at most 1000 lowercase letters. The string represents

the secret sequence of letters that opens the lock.

Output

For each test case, output a line containing a single integer with the minimum number of movements to open the lock.

Sample Input

abcxyz
abcdefghijklmnopqrstuvwxyz
aaaaaaaaa
zzzzzzzzz
zzzzbzzzz
*

Sample Output

525013



题意:
给你一个字符串,每次能够选择一个子串ss,将ss总体+1或者总体-1,a+1=b,a+1=a。要将全部字符全变为a,问最小的步数。

思路:
能够想到两种属性相反的操作(一加一减)不可能重叠,假设重叠的话。能够改动它们到不重叠也达到同样的效果。然后每一个字符就是通过一系列'+'操作或者‘-’操作(仅仅能选其1)得到a的。可是能够+或者-非常多次,不信能够看这个样例。mtezqh。答案为30.
dn[i][j]表示到将前i个字符所有变为a,最后一个字符‘-’j圈(假设j=0,仅仅需操作s[j]-'a'次)最小步数。
up[i][j]表示到将前i个字符所有变为a,最后一个字符‘+’j圈(假设j=0。仅仅需操作‘a+'26-s[j]-次)最小步数。
然后递推就可以。


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 1005
#define MAXN 200005
#define INF 0x3f3f3f3f
#define mod 20140518
#define eps 1e-6
const double pi=acos(-1.0);
typedef long long ll;
using namespace std; int n,m;
int up[maxn][40],dn[maxn][40];
char s[maxn]; void solve()
{
int i,j,k;
n=strlen(s+1);
memset(dn,0x3f,sizeof(dn));
memset(up,0x3f,sizeof(up));
dn[1][0]=s[1]-'a';
up[1][0]='a'+26-s[1];
for(i=1; i<n; i++)
{
for(j=0; j<30; j++)
{
if(s[i+1]==s[i])
{
dn[i+1][j]=dn[i][j];
up[i+1][j]=up[i][j];
}
else if(s[i+1]<s[i])
{
if(dn[i][j]<INF)
{
dn[i+1][j]=min(dn[i+1][j],dn[i][j]);
if(j>0) dn[i+1][j-1]=min(dn[i+1][j-1],dn[i][j]);
dn[i+1][j+1]=min(dn[i+1][j+1],dn[i][j]+s[i+1]-s[i]+26);
up[i+1][0]=min(up[i+1][0],dn[i][j]+'a'+26-s[i+1]);
}
if(up[i][j]<INF)
{
up[i+1][j]=min(up[i+1][j],up[i][j]+s[i]-s[i+1]);
if(j>0) up[i+1][j-1]=min(up[i+1][j-1],up[i][j]);
up[i+1][j+1]=min(up[i+1][j+1],up[i][j]+s[i]-s[i+1]+26);
dn[i+1][0]=min(dn[i+1][0],up[i][j]+s[i+1]-'a');
}
}
else
{
if(dn[i][j]<INF)
{
dn[i+1][j]=min(dn[i+1][j],dn[i][j]+s[i+1]-s[i]);
if(j>0) dn[i+1][j-1]=min(dn[i+1][j-1],dn[i][j]);
dn[i+1][j+1]=min(dn[i+1][j+1],dn[i][j]+s[i+1]-s[i]+26);
up[i+1][0]=min(up[i+1][0],dn[i][j]+'a'+26-s[i+1]);
}
if(up[i][j]<INF)
{
up[i+1][j]=min(up[i+1][j],up[i][j]);
if(j>0) up[i+1][j-1]=min(up[i+1][j-1],up[i][j]);
up[i+1][j+1]=min(up[i+1][j+1],up[i][j]+s[i]-s[i+1]+26);
dn[i+1][0]=min(dn[i+1][0],up[i][j]+s[i+1]-'a');
}
}
}
}
int ans=min(dn[n][0],up[n][0]);
printf("%d\n",ans);
}
int main()
{
while(~scanf("%s",s+1))
{
if(s[1]=='*') break ;
solve();
}
return 0;
}
/*
mtezqh
ans 30 e -26-e
*/

525013

cug oj 1479 Treasure Chest Lock (区间dp 思维)的更多相关文章

  1. 【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2101 这个dp真是神思想orz 设状态f[i, j]表示i-j先手所拿最大值,注意,是先手 所以转移 ...

  2. Light OJ 1031 - Easy Game(区间DP)

    题目大意: 给你一个n,代表n个数字,现在有两个选手,选手A,B轮流有有一次机会,每个选手一次可以得到一个或者多个数字,从左侧或者右侧,但是不能同时从两边取数字,当所有的数字被取完,那么游戏结束.然后 ...

  3. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. BZOJ 2121: 字符串游戏 区间DP + 思维

    Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对 于一个在集合S中的字符串p,如果p在L中出现,BX就可以选择是否将其删 ...

  5. Light oj 1044 - Palindrome Partitioning(区间dp)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 dp[i][j]表示i到j直接的最小回文区间个数,直接看代码 #include ...

  6. HDU 2476 String painter(区间DP+思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...

  7. 题解——洛谷P4767 [IOI2000]邮局(区间DP)

    这题是一道区间DP 思维难度主要集中在如何预处理距离上 由生活经验得,邮局放在中间显然最优 所以我们可以递推求出\( w[i][j] \)表示i,j之间放一个邮局得距离 然后设出状态转移方程 设\( ...

  8. 区间DP小结

    也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...

  9. BZOJ 2101 [Usaco2010 Dec]Treasure Chest 藏宝箱:区间dp 博弈【两种表示方法】【压维】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2101 题意: 共有n枚金币,第i枚金币的价值是w[i]. 把金币排成一条直线,Bessie ...

随机推荐

  1. dotnet core 运行程序注意事项

    .net core 程序 debug版本无法直接运行,因为没有相关的dll,只有在进入项目文件夹那一层,执行 dotnet run 来运行,会自动链接 当前用户的个人文件夹下的 .nuget/.pac ...

  2. day19-IO多路复用

    1.I/O多路复用指:通过一种机制,可监听多个描述符(soket对象)(文件句柄),一旦某个描述符发送编号(一般指读就绪或写就绪),能够通知程序进行相应的读写操作. 2.I/O多路复用方式:selec ...

  3. 《Spark Python API 官方文档中文版》 之 pyspark.sql (四)

    摘要:在Spark开发中,由于需要用Python实现,发现API与Scala的略有不同,而Python API的中文资料相对很少.每次去查英文版API的说明相对比较慢,还是中文版比较容易get到所需, ...

  4. hdoj--2082<母函数>

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=2082题目描述:26个字母各有价值,分别是1到26:给出每个字母的个数,求单词价值不超过50 的单词 ...

  5. IE6 IE7下li间距、高度不一致问题(转)

    http://www.phpddt.com/dhtml/926.html 问题描述:li的高度在IE6 IE7间距高度和其他浏览器不一致,即便设定了高度,IE6,7中,仍比其他浏览器要高. 解决方法: ...

  6. Hadoop High Availability

    Hadoop High Availability HA(High Available), 高可用,是保证业务连续性的有效解决方案, 通常通过设置备用节点的方式实现; 一般分为执行业务的称为活动节点(A ...

  7. javaweb-简单的验证码和算术验证码

    我们登陆网站时,每次都会填写一些验证码,这些验证码的作用:防止被恶意攻击,验证码上面的字母数字一般都是随机生成的,因此我们首先要写一个方法生成一个随机的字符串,这里就需要java里面的随机函数Rand ...

  8. NOIP2017赛前模拟(3):总结

    题目: 1.购买板凳(100) 大意:区间修改最后查询全局最大值; 2.新排序(40分暴力) 大意:给一串长度小于100000的数列···每次操作找出序列中严格小于其左边的数字或者严格大于其右边的数字 ...

  9. Java数据库连接JDBC用到哪种设计模式?

    还没看桥接模式,占tag 桥接模式: 定义 :将抽象部分与它的实现部分分离,使它们都可以独立地变化. 意图 :将抽象与实现解耦. 桥接模式所涉及的角色 1.  Abstraction :定义抽象接口, ...

  10. docker (centOS 7) 使用笔记4 - etcd服务

    本次测试的系统包含centos 7.2 64 bit,centos 7.3 64 bit 1. 安装 yum -y install etcd 2. 配置 此处一共准备了3台机器(10.10.10.10 ...