Codeforces Round #376 (Div. 2) A. Night at the Museum__codeforces

Source: Internet
Author: User
Tags abs string find
A. Night at the Museum time limit/test 1 second memory limit per test 256 megabytes input standard input output Standa RD output

Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At the ' Whole exposition ' of the ' the ' and ' to take ' of the night he receivedembosser and is.

Embosser is a special devise which allows to "print" the text of a plastic tape. The Text is printed sequentially and character by character. The device consists of a wheel with a lowercase 中文版 letters written in a circle, static pointer to the and a button that is 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 '. Letters are located as shown on the picture:

After Grigoriy add new "to" the "base he" has to print its name on the plastic tape and attach it to the corresponding ex Hibit. It ' s not required to return the wheel to its initial position with pointer on the letter ' a '.

Our hero are afraid that some exhibits could become alive and start to attack him, so him wants to print the names as fast as Possible. Help him, for the "given string find" minimum number of rotations of the wheel to print it. Input

The only line of input contains the name of some exhibit-the non-empty string consisting to no more than characters. It's guaranteed that the string consists of lowercase 中文版 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 it would is 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 ' (rotations counterclockwise ), from ' U ' to ' s ' (2 counterclockwise rotations).

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

Ideas:

To know the number of times you need to rotate from the last character to the next one, you should know if you rotate clockwise or counterclockwise, if you rotate clockwise, you will be equal to the last number minus the absolute value of the previous number, or rotate clockwise 26 counterclockwise. The idea was simple, but at first he was a disc, not clockwise or counterclockwise.

Code:

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <vector>
#include <string.h>
#include <map>
#define INF 0x3f3f3f3f3f
using namespace std;
int main ()
{
    char a[1001];
    while (~SCANF ("%s", a))
    {
        int l=strlen (a);
        Char ans= ' a ';
        int sum=0;
        for (int i=0;i<l;i++)
        {
            if (ABS (A[i]-ans) >13)
            {
                sum+= (26-abs (A[i]-ans));
            }
            else
            {
                sum+=abs (A[i]-ans);
            }
            Ans=a[i];
        }
        printf ("%d\n", sum);
    }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.