#abc223b. B - String Shifting
B - String Shifting
Score : points
问题描述
在一个非空字符串上,左移操作会将第一个字符移动到字符串的末尾,而右移操作则会将最后一个字符移动到字符串的开头。
例如,对abcde
进行一次左移操作得到的结果是bcdea
,对abcde
进行两次右移操作得到的结果是deabc
。
给定一个仅包含小写英文字母且非空的字符串。在通过执行任意次数的左移和任意次数的右移操作可能得到的所有字符串中,找出字典序最小的字符串和字典序最大的字符串。
什么是字典序?
简单来说,字典序就是字典中单词排列的顺序。更正式地讲,以下是一个确定不同字符串和之间的字典序的算法。
以下,令表示字符串的第个字符。另外,若字典序小于,我们记作;若字典序大于,我们记作。
- 计算和长度中的较小值并赋给。对于每个,检查和是否相同。
- 如果存在某个使得,取其中最小的这样的作为。然后比较和。如果在字母表顺序上先于出现,则判定并结束比较;若后于出现,则判定并结束比较。
- 若不存在满足的,则比较和的长度。若比短,则判定并结束比较;若比长,则判定并结束比较。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
On a non-empty string, a left shift moves the first character to the end of the string, and a right shift moves the last character to the beginning of the string.
For example, a left shift on abcde
results in bcdea
, and two right shifts on abcde
result in deabc
.
You are given a non-empty consisting of lowercase English letters. Among the strings that can be obtained by performing zero or more left shifts and zero or more right shifts on , find the lexicographically smallest string and the lexicographically largest string.
What is the lexicographical order?
Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings and .
Below, let denote the -th character of . Also, if is lexicographically smaller than , we will denote that fact as ; if is lexicographically larger than , we will denote that fact as .
- Let be the smaller of the lengths of and . For each , we check whether and are the same.
- If there is an such that , let be the smallest such . Then, we compare and . If comes earlier than in alphabetical order, we determine that and quit; if comes later than , we determine that and quit.
- If there is no such that , we compare the lengths of and . If is shorter than , we determine that and quit; if is longer than , we determine that and quit.
Constraints
- consists of lowercase English letters.
- The length of is between and (inclusive).
Input
Input is given from Standard Input in the following format:
Output
Print two lines. The first line should contain , and the second line should contain . Here, and are respectively the lexicographically smallest and largest strings obtained by performing zero or more left shifts and zero or more right shifts on .
Sample Input 1
aaba
Sample Output 1
aaab
baaa
By performing shifts, we can obtain four strings: aaab
, aaba
, abaa
, baaa
. The lexicographically smallest and largest among them are aaab
and baaa
, respectively.
Sample Input 2
z
Sample Output 2
z
z
Any sequence of operations results in z
.
Sample Input 3
abracadabra
Sample Output 3
aabracadabr
racadabraab
update @ 2024/3/10 09:49:12