The second provincial match of the 11th Blue Bridge Cup python group-sorted

1. Problem description:

Xiaolan recently learned some sorting algorithms, among which bubble sorting impressed him. In bubble sorting, only two adjacent elements can be exchanged at a time. Xiaolan found that if the characters in a string are sorted and only two adjacent characters are allowed to be exchanged, the total number of exchanges for bubble sorting is the least among all possible sorting schemes. For example, for sorting the string lan, only 1 exchange is required. For the string qiao sorting, a total of 4 exchanges are required. Xiaolan found a lot of strings and tried to sort them. He happened to come across a string that needed 100 exchanges, but he forgot to write down this string, and now he can't find it. Please help Xiaolan find a string that only contains lowercase English letters and no repetitions of letters. Sorting the characters of this string requires exactly 100 exchanges. If you can find more than one, please tell Xiaolan the shortest one. If there are still multiple shortest ones, please tell Xiaolan the one with the smallest lexicographical order. Please note that the same characters cannot be included in the string.

[Answer Submission]
This is a question that fills in the blanks with the result. You only need to calculate the result and submit it. The result of this question is a string containing only lowercase English letters. When submitting the answer, only fill in this string, and fill in the extra content will not be scored.

2. Thinking analysis:

Because it is necessary to use a string of shorter length as much as possible to form 100 times of adjacent character exchanges, and the dictionary order is required to be the smallest and only adjacent letters can be exchanged, so we need to use as much as possible to use the front Characters, and use the reverse order, considering the complete reverse order, so that the number of exchanges of the string can be made as much as possible, that is, the shortest string, then a fully reversed string with a length of 15 is required, because the number of complete reverse exchanges is: (1 + 2 + ... 14) * 14 = 105, so we need to adjust the string in completely reverse order: "onmlkjihgfedcba" to form a string of adjacent letters that can be used 100 times, 105 The number of times is only 5 times more than 100 times, so we need to eliminate the extra 5 times. By analyzing the characteristics of the string, we can know that the characters with larger lexicographic order are adjusted forward, and the relative position of the string in front of it is It’s unchanged, so we need to put the 6th letter of the reversed "onmlkjihgfedcba" string to the front, so that when "abcdefghi" is arranged, j is behind it, so that the position of the letter j does not need to be adjusted, so Exactly eliminate the extra 5 times, and the final number of exchanges is 14 + 13 + ... + 6 + 4 + 3 + 2 + 1 = 100 times. So the result is: jonmlkihgfedcba. The main thing is to find out the rules and consider the extreme conditions before making adjustments.

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/114963102