Codeforces Round # 376A (div2 ),

Source: Internet
Author: User

Codeforces Round # 376A (div2 ),

Night at the Museum

Question:

There is a turntable with 26 English letters evenly distributed on the edge of the turntable. The rotor is initially at the letter a and can only rotate to adjacent letters at a time, then, enter a string consisting of 26 letters (up to 100 characters) and rotate it to the corresponding letter position without resetting. Q: How many times do I need to convert it in total? Programming implementation.

Input:

Enter a string of up to 100 English letters.

Output:

The number of times the image is rotated (an integer ).

 

Solution thinking:

Because the section "... za..." in a circle of letters is not continuous, we need to discuss the situation in detail. I did this:

The pivot is connected to the center of the pointer. the right side of the axis (<pointer, pointer + 13 or pointer-13>), except when the pivot points to 'A, at other times, there must be one letter on both sides of the axis, and the other letter cannot be consecutive (including "za" string ).

The left and right sides of the axis are placed vertically, and the needle is on the top. At this time, the left and right sides of the axis are placed.

It is not hard to see that pointer takes 'n' as the boundary. When the value is less than 'n', the discontinuous section is on the left side of the axis, and when the value is greater than 'n', the discontinuous section is on the right side of the axis. For example:

Final code:

 

1 # include <iostream> 2 # include <string> 3 using namespace std; 4 5 int main (void) 6 {7 int clockLetter (string c); 8 string s; 9 int numStep; 10 cin> s; 11 12 numStep = clockLetter (s); 13 14 cout <numStep <endl; 15 return 0; 16} 17 18 int clockLetter (string c) 19 {20 int n = 0; // number of steps, initial value: 0 21 int I; 22 char pointer = 'a '; // The initial value of the needle is 'A' 23 24 for (int I = 0; I <c. length (); I ++) 25 (<Pointer, pointer + 13 or pointer-13>), except when the probe points to 'A, at other times, there must be one letter on both sides of the axis, and the other letter cannot be consecutive (including "za" string ). 26 // the left and right sides of the axis are placed vertically, and the needle is placed at the top. At this time, the right side of the axis is 27 if (pointer <= 'n ') // at this time, the left Letter of the axis is not consecutive 28 {29 if (c [I]> = pointer & c [I] <= pointer + 13) // continuous segment 30 {31 n + = c [I]-pointer; 32} 33 else if (c [I]> pointer + 13) // discontinuous Section 1 34 {35 n + = 26-(c [I]-pointer); 36} 37 else if (c [I] <pointer) // discontinuous section 2 38 {39 n + = pointer-c [I]; 40} 41} 42 else if (pointer> 'n ') // at this time, the letter on the right of the axis is not 43 {44 if (c [I] <= pointer & c [I]> = pointer-13) // continuous segment 45 {46 n + = pointer-c [I]; 47} 48 else if (c [I]> pointer) // The Discontinuous section 349 {50 n + = c [I]-pointer; 51} 52 else if (c [I] <pointer-13) // discontinuous section 4 53 {54 n + = 26-(pointer-c [I]); 55} 56} 57 58 pointer = c [I]; // when turning the next letter, let the pointer stop at the current position without resetting 59} 60 61 return n; 62}
C ++ Code

 

 

 

Result test:

 

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.