题目链接: http://codeforces.com/contest/731/problem/A

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 received embosser 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.

题解:

很基础的一道题, 但是值得积累。

对于一条循环的数轴,就以0~9的数轴为例(9跟在0的后面,构成循环), 求now到next的最短距离:

情况1:不需要从尾越过头, 或者从头越过尾, dis = abs(next - now)。

情况2:需要从尾越过头, dis = (len-now) + next 。 第一部分为从now到尾的距离, 第二部分为从头到next的距离。

情况3:需要从头越到尾, dis = now + (len-next)。 第一部分为从now到头的距离, 第二部分为从尾到next的距离。

综上, 所以:最短距离 = min( abs(next-now), min( (len-now)+next, now+(len-next) ) )

代码如下:

#include <bits/stdc++.h>
using namespace std; int main()
{
char s[1000];
cin>>s;
int ans = 0, now = 0;
for(int i = 0; i<strlen(s); i++)
{
int next = s[i] - 'a';
ans += min( abs(now-next), min( now+26-next, 26-now+next ) );
now = next;
}
cout<<ans<<endl;
}

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

  1. 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 ...

  2. 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 ...

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

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

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

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

  5. Codeforces Round #376 (Div. 2) F. Video Cards 数学 & 暴力

    http://codeforces.com/contest/731/problem/F 注意到一个事实,如果你要找一段区间中(从小到大的),有多少个数是能整除左端点L的,就是[L, R]这样.那么,很 ...

  6. Codeforces Round #376 (Div. 2) F. Video Cards —— 前缀和 & 后缀和

    题目链接:http://codeforces.com/contest/731/problem/F F. Video Cards time limit per test 1 second memory ...

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

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

  8. Codeforces Round #376 (Div. 2)

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

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

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

随机推荐

  1. Oracle中PL/SQL 范例

    1.写匿名块,输入三角形三个表的长度.在控制台打印三角形的面积 declare v_side_first ):=&第一条边; v_side_second ):=&第二条边; v_sid ...

  2. 机器学习之SVM

    一.线性分类器: 首先给出一个非常非常简单的分类问题(线性可分),我们要用一条直线,将下图中黑色的点和白色的点分开,很显然,图上的这条直线就是我们要求的直线之一(可以有无数条这样的直线) 假如说,我们 ...

  3. 智能手机+DIY红外=万能遥控器

    目前好像只有:三星S4.,努比亚大牛,华为荣耀3等几款新机才有红外遥控功能,那我们使用的手机没有这个功能怎么办?不要急我有办法呵呵,本次DIY材料好找又简单,大家都可以亲自试一试! DIY材料:红外二 ...

  4. ActiveX控件打包成Cab置于网页中自动下载安装 [转]

    http://blog.sina.com.cn/s/blog_520c32270100nopj.html 做过ActiveX控件的朋友都知道,要想把自己做的ActiveX控件功能放在自己的网页上使用, ...

  5. dll的使用

    2016-12-11   23:02:24 一:生成DLL 1:创建DLL工程 文件->新建->项目->visual c++->win32->win32控制台应用程序(w ...

  6. 怎样让Eclipse的智能提示像VS一样霸气

    说起来用Eclipse也有一段时间了.相信每个用过的人都知道他的智能提示功能真的是糟糕透了,与VisualStudio2008简直不是一个档次的!我就纳闷了,他为什么不弄好一点呢.今天我实在是忍不住了 ...

  7. Atitit.ati&#160;&#160;str&#160;&#160;字符串增强api

    Atitit.ati  str  字符串增强api 1. java StringUtils方法全览 分类: Java2011-11-30 17:22 8194人阅读 评论(2) 收藏 举报 javas ...

  8. linux c语言 select函数使用方法

    linux c语言 select函数使用方法 表头文件 #i nclude<sys/time.h> #i nclude<sys/types.h> #i nclude<un ...

  9. ORCAD元件属性白色区域和黄色区域的理解

    白色部分为instance属性,黄色部分为occurence 属性 在平坦式电路中,黄色部分是默认不显示的. 在层次式电路中,黄色部分会显示.      如果这两个区域的Reference不同,以黄色 ...

  10. 网页编程-Django(一)

    业内: GET:获取数据 POST:提交数据 上传单个数据: request.POST.get(‘’name名‘’) 上传多选数据: request.POST.getlist('name名') 上传文 ...