time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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:

from ‘a’ to ‘z’ (1 rotation counterclockwise),

from ‘z’ to ‘e’ (5 clockwise rotations),

from ‘e’ to ‘u’ (10 rotations counterclockwise),

from ‘u’ to ‘s’ (2 counterclockwise rotations).

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

【题解】



模拟题;

可以一个一个地加;

也可以一个一个地减;

看看到达目标哪个需要的操作数较小就累加上那个操作数就好;

#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std; char now;
char s[200];
int step = 0; int add(char key,char goal)
{
int cnt = 0;
while (key != goal)
{
key++;
cnt++;
if (key > 'z')
key = 'a';
}
return cnt;
} int sub(char key, char goal)
{
int cnt = 0;
while (key != goal)
{
key--;
cnt++;
if (key < 'a')
key = 'z';
}
return cnt;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
now = 'a';
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i <= len - 1; i++)
if (s[i] == now)
continue;
else
{
char temp = now;
int way1 = add(temp,s[i]);
temp = now;
int way2 = sub(temp, s[i]);
step += min(way1, way2);
now = s[i];
}
printf("%d\n", step);
return 0;
}

【87.65%】【codeforces 731A】Night at the Museum的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【45.65%】【codeforces 560B】Gerald is into Art

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 734F】Anton and School

    [题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...

  4. 【codeforces 65A】Harry Potter and Three Spells

    [题目链接]:http://codeforces.com/problemset/problem/65/A [题意] 你有3种魔法; 1.可以将a单位的石头变成b单位的铅 2.可以将c单位的铅变成d单位 ...

  5. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  6. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  7. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  8. 【链表】【模拟】Codeforces 706E Working routine

    题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...

  9. 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

    题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...

随机推荐

  1. js进阶 13-5 jquery队列动画如何实现

    js进阶 13-5 jquery队列动画如何实现 一.总结 一句话总结:同一个jquery对象,直接写多个animate()就好. 1.什么是队列动画? 比如说先左再下,而不是左下一起走 2.怎么实现 ...

  2. html 代码

    1.结构性定义 文件类型 <HTML></HTML> (放在档案的开头与结尾) 文件主题 <TITLE></TITLE> (必须放在「文头」区块内) 文 ...

  3. synchronized和ReentrantLock区别

    一.什么是sychronized sychronized是java中最基本同步互斥的手段,可以修饰代码块,方法,类. 在修饰代码块的时候需要一个reference对象作为锁的对象. 在修饰方法的时候默 ...

  4. pat 2-05. 求集合数据的均方差(水题)

    代码: #include<cstdio> #include<iostream> #include<cmath> using namespace std; doubl ...

  5. WPF中实现验证码

    原文:WPF中实现验证码 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79563449 W ...

  6. linux系统 wm9713声卡配置 记录

    近期在搞wm9713的外音播放和耳机的检測,搞了几天不负所望,最终搞定了.现记录例如以下,开发板为real210 v6.2版本号: 假设要让外音输出,那么确保wm9713的0x1c寄存器的值为0x12 ...

  7. error C2220: warning treated as error - no 'object' file generated warning C4819: The file contains a character that cannot be represented in the current code page (936).

    用Visual Studio2015 编译时,遇到如下编译错误: error C2220: warning treated as error - no 'object' file generated ...

  8. Voronoi Diagram——维诺图

    Voronoi图定义   任意两点p 和q 之间的欧氏距离,记作 dist(p, q) .就平面情况而言,我们有           dist(p, q) =  (px-qx)2+ (py-qy)2 ...

  9. chain rule 到 Markov chain

    1. 联合概率(joint distribution)的链式法则 基于链式法则的 explicit formula: p(x1:n)===p(x)p(x1)∏i=2np(xi|x1,-,xi−1)∏i ...

  10. angular动画知识点以及代码样例

    原文地址 https://www.jianshu.com/p/4400174072e2 大纲 1.angular动画的相关概念 2.angular动画的绑定方式 3.angular动画代码实例 1.a ...