Night at the Museum

原创 2018年04月17日 20:30:16
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
Copy
zeus
Output
Copy
18
Input
Copy
map
Output
Copy
35
Input
Copy
ares
Output
Copy
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.

http://codeforces.com/problemset/problem/731/A
#include<iostream>
using namespace std;
char a[]="abcdefghijklmnopqrstuvwxyz";   
int main ()
{
    string s;
	while(cin>>s)
	{
		int now=0,next,ans=0;
		for(int i=0;i<s.size();i++)
		{
			next=s[i]-'a';
		    ans+=min(abs(next-now),min((now+26-next),(26-now+next)));
		    now=next;	
		} 
	cout<<ans<<endl;	
	} 	
} 

题解:

很基础的题

有三种情况需要判别:

1、直接相减首尾距离abs(next-now),例如:a->c

2、从后向前now+26-next   例如 z->c

3、从头到尾都要  例如  b->x

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_39618052/article/details/79979902

CodeForces 731A Night at the Museum(博物馆循环的单词表)

http://codeforces.com/problemset/problem/731/A A. Night at the Museum time limit per test 1...
  • shyazhut
  • shyazhut
  • 2016-10-19 17:53:57
  • 541

A. Night at the Museum

time limit per test 1 second memory limit per test 256 megabytes input standard input ...
  • jj12345jj198999
  • jj12345jj198999
  • 2016-10-24 21:21:24
  • 493

Night at the Museum(水题)

A B C D E F A - Night at the Museum Time Limit:1000MS     Memory Limit:262144KB     64bit IO Forma...
  • CSDNyuanyuan
  • CSDNyuanyuan
  • 2016-10-19 16:16:32
  • 456

Codeforces-731A A. Night at the Museum

A. Night at the Museumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inp...
  • qq_41333844
  • qq_41333844
  • 2018-04-11 20:21:07
  • 9

CodeForces 731A 之 Night at the Museum

Description Grigoriy, like the hero of one famous comedy film, found a job as a night security gu...
  • ESESZB
  • ESESZB
  • 2016-10-24 11:34:07
  • 136

A - Night at the Museum

Description Grigoriy, like the hero of one famous comedy film, found a job as a night security guar...
  • xuptacm
  • xuptacm
  • 2016-11-18 20:11:03
  • 190

codeforces 731A Night at the Museum

点击打开链接 A. Night at the Museum time limit per test 1 second memory limit per test 25...
  • zhang__liuchen
  • zhang__liuchen
  • 2017-07-27 14:54:28
  • 80

Codeforce 731A. Night at the Museum

链接http://codeforces.com/problemset/problem/731/A思路 对两个字母ch1,ch2顺序和逆序走一遍,取较小值#includechar m[]="abcde...
  • sUb_569
  • sUb_569
  • 2016-10-19 22:45:13
  • 157

CodeForces 731A Night at the Museum

A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes ...
  • yao1373446012
  • yao1373446012
  • 2016-10-22 17:24:16
  • 159

codeforces 731A Night at the Museum

题目链接:http://codeforces.com/problemset/problem/731/A 题意:给出一串字符串,求每个两个相邻字母之间的距离的和。距离定义 为下图上的劣弧长。第一个字母...
  • Fistice
  • Fistice
  • 2016-10-16 20:57:07
  • 328
收藏助手
不良信息举报
您举报文章:Night at the Museum
举报原因:
原因补充:

(最多只允许输入30个字)