A. Night at the Museum

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At first night he receivedembosser and was to take stock of the whole exposition.

Embosser is a special devise that allows to "print" the text of a plastic tape. Text is printed sequentially, character by character. The device consists of a wheel with a lowercase English letters written in a circle, static pointer to the current letter and a button that print the chosen letter. At one move it's allowed to rotate the alphabetic wheel one step clockwise or counterclockwise. Initially, static pointer points to letter 'a'. Other letters are located as shown on the picture:

After Grigoriy add new item to the base he has to print its name on the plastic tape and attach it to the corresponding exhibit. It's not required to return the wheel to its initial position with pointer on the letter 'a'.

Our hero is afraid that some exhibits may become alive and start to attack him, so he wants to print the names as fast as possible. Help him, for the given string find the minimum number of rotations of the wheel required to print it.

Input

The only line of input contains the name of some exhibit — the non-empty string consisting of no more than 100 characters. It's guaranteed that the string consists of only lowercase English letters.

Output

Print one integer — the minimum number of rotations of the wheel, required to print the name given in the input.

Examples

input

zeus

output

18

input

map

output

35

input

ares

output

34

Note

To print the string from the first sample it would be optimal to perform the following sequence of rotations:

  1. from 'a' to 'z' (1 rotation counterclockwise),

  2. from 'z' to 'e' (5 clockwise rotations),

  3. from 'e' to 'u' (10 rotations counterclockwise),

  4. from 'u' to 's' (2 counterclockwise rotations).

In total, 1 + 5 + 10 + 2 = 18 rotations are required.

------

题目很水,大意:

指针一开始停到a,然后需要拨到所给的字符串,问需要拨几下走几个格子如zeus 从a走到z需要1,z再走到e需要……(可顺可逆,取最小) 。

最后得需要18次。

(这次英语太渣了,题目背景没看懂,靠看数据猜题意……)

——————

AC代码 :

#include<cstdio>

#include<iostream>

#include<cmath>

#include<algorithm>

using namespace std; 

int main()

{

char s ; 

char now = 'a' ;

int ans = 0 ; 

while ( cin >> s ) 

{

//cout << s << endl ;

ans +=  min ( abs(s - now)   , abs( 26 - abs( s - now ) ) );

now = s ; 

 } 

cout << ans << endl ;

return 0 ; 

}

这道题之所以写上来是因为自己实在太蠢了。

一开始一直想不到怎么处理顺时针逆时针怎么取最小?

一开始甚至考虑了 min( abs( s - now ) , abs(  now + 'z' - 'a' +1 - s )  这么蠢的代码。。

后来感觉这个和约瑟夫环很像,于是考虑用取模,写成了

min( abs ( s - now ) , abs( s - now ) % 26 )

后来发现数据就没有大于26的……怎么取都不出来,然后脑残的还想要不要加一个常数来使这个方法可用。。。

最后突然想到,一般这种情况,对于大于一圈的用mod 小于一圈的用减法就可以了啊! 被自己蠢哭了。

不过也算一个教训吧,大于一圈以上再考虑mod 小于一圈完全可行。

min( abs( s - now ) , abs( 26 - abs( s - now ) ) ;完破( 本来第二个表达式用了嵌套的abs是怕 里面答案出现负数,现在感觉我又冗余了……)

A. Night at the Museum Round#376 (Div. 2)的更多相关文章

  1. Codeforces Round #376 (Div. 2) A. Night at the Museum —— 循环轴

    题目链接: http://codeforces.com/contest/731/problem/A A. Night at the Museum time limit per test 1 secon ...

  2. Codeforces Round #376 (Div. 2) A B C 水 模拟 并查集

    A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. Codeforces Round #376 (Div. 2) D. 80-th Level Archeology —— 差分法 + 线段扫描法

    题目链接:http://codeforces.com/contest/731/problem/D D. 80-th Level Archeology time limit per test 2 sec ...

  4. 【Codeforces】Round #376 (Div. 2)

    http://codeforces.com/contest/731 不发题面了,自己点链接 总结一下 考场上 原以为这次要加很多raiting... 但FST狗记邓,只加了58rating 总结一下 ...

  5. Codeforces Round #376 (Div. 2)

    A 模拟 #include <cstdio> #include <cstring> int Ans; ]; inline ?x:-x;} inline int Min(int ...

  6. Codeforces Round #376 (Div. 2) C D F

    在十五楼做的cf..一会一断...比赛的时候做出了ABCF 就没有时间了 之后没看题解写出了D..E是个神奇的博弈(递推或者dp?)看了题解也没有理解..先写了CDF.. C 有n个袜子 每个袜子都有 ...

  7. Codeforces Round #376 (Div. 2) C题 Socks(dsu+graphs+greedy)

    Socks Problem Description: Arseniy is already grown-up and independent. His mother decided to leave ...

  8. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  9. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

随机推荐

  1. foreach绑定

    目的 foreach可以将一个数组中的实体循环的进行绑定.这在将一个list显示成table时非常有用. 假设数组是observable的,当在绑定后做了add, remove,或者重新排序后,绑定会 ...

  2. 一个IT人士的个人经历,给迷失方向的朋友(转)

    作者: 果子狸  来源: WP开发者社区  发布时间: 2013-11-08 10:43  阅读: 50078 次  推荐: 494   原文链接   [收藏] 这些日子我一直在写一个实时操作系统内核 ...

  3. Quartz+log4net实现控制台程序定时运行,并且记录日志

    c# 控制台程序定时运行,并记录日志. 组件Quartz + log4net Quartz定时运行程序,log4net记录日志. 首先建立控制台程序"QuartzTest" 安装Q ...

  4. GD库 图片缩略图 图片水印

    /** * GD库 图片缩略图 *//*$image = imagecreatefromjpeg("1.jpg");var_dump($image);exit;$width = i ...

  5. WEB服务器:Apache、Tomcat、JBoss、WebLogic、Websphere、IIS的区别与关系

    1)Apache  免费,世界使用排名第一的Web服务器.它可以运行在几乎所有广泛使用的计算机平台上.Apache的特点是简单.速度快.性能稳定,并可做代理服务器来使用.Apache是以进程为基础的结 ...

  6. appium元素集合定位

    概念:某一类型元素的集合 list对象 来源:-控件复用 -相同的控件类型 -取名重复 语法:driver.findElements(By.id("text1")).get(0) ...

  7. Note_JavaWeb_MyBatis3

    Jar包 mybatis-3.2.8.jar junit4.4.jar log4j-1.2.17.jar 常用类 Resources SqlSession SqlSessionFactory SqlS ...

  8. linux强制svn提交时必须写注释

    打开hooks,然后将pre-commit.tmpl修改为pre-commit,打开pre-commit,写入如下代码: #!/bin/sh REPOS="$1" TXN=&quo ...

  9. hadoop 常用配置项【转】

    hadoop 常用配置项[转] core-site.xml  name value  Description   fs.default.name hdfs://hadoopmaster:9000 定义 ...

  10. wxpython 安装教程

    wxpython在windows 上的安装,需要在wxpython官网上下载对应的版本:Python分为32和64位系统不是系统的32位和64位 所以可以先在IDE 下输入Python看下当前是32还 ...