【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 < ...
随机推荐
- RMAN-03002、RMAN-06059
使用RMAN备份的时候无法正常备份,抛出以下错误: RMAN-03002: failure of backup command at 04/20/2015 18:55:45 RMAN-06059: e ...
- 使用H5 formData对象上传图片和视频的文件时,必填的属性
async : false,cache : false,contentType : false,// 告诉jQuery不要去设置Content-Type请求头processData : false,/ ...
- 【POJ 1226】Substrings
[链接]h在这里写链接 [题意] 给你n个字符串. 让你找一个字符串s. 设s'为这个字符串的逆序. 要求s或者s'在每个字符串里都能够找得到. 并且要求s的长度最长. 求出这个最长的串的长度. [题 ...
- 取消UITableViewCell的背景色
//无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色,也就是系统默认的颜色 cell.selectionStyle = ...
- 15、USB摄像头图片采集+QT显示
一.Qt的下载和的安装 关于Qt的安装,网络上有很详细的介绍.这里只做简单介绍. 需要的安装包一共有两个:Qt Creator 和QTE. 1)QT Creator 下载地址:qt-sdk-linux ...
- javascript 调用C++函数
分3步: 一>实现IDispatch 接口 #ifndef _IDISPIMP_H_ #define _IDISPIMP_H_ // idispimp.h class CImpIDispatch ...
- [RxJS] Hot Observable, by .share()
.share() is an alias for .publish().refCount(). So if the source is not yet completed, no matter how ...
- [Angular2 Form] Model Driven Form Custom Validator
In this tutorial we are going to learn how simple it is to create custom form field driven validator ...
- 机器学习01-kNN邻近算法
k-近邻算法 概述:k-近邻算法採用測量不同特征值之间的距离方法进行分类 长处:精度高.对于异常值不敏感.无数据输入假定 缺点:计算复杂度高,空间复杂度高,而且它没有办法各处基础数据的一些内部信息数据 ...
- 批量杀死MySQL连接的几种方法
法一: 通过information_schema.processlist表中的连接信息生成需要处理掉的MySQL连接的语句临时文件,然后执行临时文件中生成的指令. mysql> select ...