首页 技术 正文
技术 2022年11月16日
0 收藏 392 点赞 4,363 浏览 2401 个字

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:

A. Night at the Museum Round#376 (Div. 2)

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

A. Night at the Museum Round#376 (Div. 2)

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是怕 里面答案出现负数,现在感觉我又冗余了……)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905