120k views
0 votes
Problem Description

A picnic to a famous museum is being planned in a school for class VI. When they reached the spot, the students started quarreling among themselves in the queue. So the teacher came up with an idea of "good string" which is explained below.

Good String is provided as input. All letters in this string are good letters. Good letters need to be used in further computations as explained below.

The teacher asked all the students to convert their names into good names with the help of good string. While converting, they have to calculate the distance. Based on that, she will arrange the students in a queue.

For converting a name into good name, for each letter i in the name, select the nearest letter from the good name. Distance is calculated as the differences between the ASCII values of i and selected good letter. If there are two letters which are equidistant from i, select the letter which is nearest to the previously used good letter. In that case, distance will be the difference of ASCII value of previously used good letter and selected letter. If i is already present in the good string then no need to change it. Initially, previous good letter will be the first letter of good string. Calculate the total distance of the given name.

Given the name of the student who is confused of implementing this task. Help him to calculate the total distance for his name.

Note: Letters from good string can be reused any number of times.

User Bznein
by
8.1k points

1 Answer

4 votes

Final answer:

To calculate the total distance for your name, you need to convert each letter to the nearest letter from the good string and calculate the distance between them. If a letter is already present in the good string, no conversion is needed. Add up the distances to get the total distance.

Step-by-step explanation:

To calculate the total distance for your name, you need to convert each letter in your name to the nearest letter from the good string provided. The distance is calculated as the difference between the ASCII values of the two letters. If two letters are equidistant, choose the one closest to the previously used good letter. If a letter is already present in the good string, there is no need to change it.

Here is a step-by-step explanation:

  1. Initialize a variable 'previousGoodLetter' to the first letter of the good string and set the 'totalDistance' to 0.
  2. Iterate through each letter in your name.
  3. If the letter is already present in the good string, skip to the next letter.
  4. Find the nearest letter in the good string by calculating the distance from the current letter to each letter in the good string.
  5. Select the letter that has the minimum distance. If there are multiple letters with the same distance, choose the one closest to the previous good letter.
  6. Update the 'previousGoodLetter' to the selected letter.
  7. Add the distance between the current letter and the selected good letter to the 'totalDistance'.

The 'totalDistance' obtained at the end will be the total distance for your name.

User Aman Deep Gautam
by
8.9k points