【87.65%】【codeforces 731A】Night at the Museum
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的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【45.65%】【codeforces 560B】Gerald is into Art
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
- 【codeforces 65A】Harry Potter and Three Spells
[题目链接]:http://codeforces.com/problemset/problem/65/A [题意] 你有3种魔法; 1.可以将a单位的石头变成b单位的铅 2.可以将c单位的铅变成d单位 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【链表】【模拟】Codeforces 706E Working routine
题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...
随机推荐
- CC2530定时器使用
定时器学习 文件夹 说明 依据数据手冊可知CC2530总共同拥有4个定时器,可是定时器2被系统占用,可用的仅仅有三个,分别为定时器1/3/4 Timer在协议栈的代码位置为hal_timer ...
- (最新)使用爬虫刷CSDN博客访问量——亲测有效
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 1.概述 前言:前两天刚写了第一篇博客https://blog.csdn.net/qq_41782425/article/deta ...
- Web自动化测试 Selenium+Eclipse+Junit+TestNG+Python
Selenium+Eclipse+Junit+TestNG+Python 第三步 下载Selenium IDE.SeleniumRC.IEDriverServer.SeleniumClient Dri ...
- Surging 微服务框架使用入门
原文:Surging 微服务框架使用入门 前言 本文非 Surging 官方教程,只是自己学习的总结.如有哪里不对,还望指正. 我对 surging 的看法 我目前所在的公司采用架构就是类似与Sur ...
- 参数传递方法(用Delphi的汇编代码解释)
参数传递方法 李纬的InsideVCL<第一章>中提到Windows定义的回调函数 typedef LRESULT (CALLBACK*WNDPROC)(HWND,UNIT,WPARAM, ...
- C语言结构体的字节对齐原则
为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这就需要各种类型数据 ...
- 【例题 6-1 UVA - 210】Concurrency Simulator
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 队列模拟题. 注意初始化.. 然后题目中是让读入一个数据组数然后再输入数据的. 但样例..但样例没有!? [代码] #include ...
- [array] leetCode-15. 3Sum-Medium
leetCode-15. 3Sum-Medium descrition Given an array S of n integers, are there elements a, b, c in S ...
- 10、bitmap格式分析
说到图片,位图(Bitmap)当然是最简单的,它Windows显示图片的基本格式,其文件扩展名为*.BMP.在Windows下,任何各式的图片文件(包括视频播放)都要转化为位图个时候才能显示出来,各种 ...
- 【rlz03】十六进制转十进制
Time Limit: 3 second Memory Limit: 2 MB 问题描述 输入一个十六进制数,编程转换为十进制数. 整数部分不会超过65535,十六进制的小数部分不会超过2位. Sam ...