#abc223b. B - String Shifting

B - String Shifting

Score : 200200 points

问题描述

在一个非空字符串上,左移操作会将第一个字符移动到字符串的末尾,而右移操作则会将最后一个字符移动到字符串的开头。

例如,对abcde进行一次左移操作得到的结果是bcdea,对abcde进行两次右移操作得到的结果是deabc

给定一个仅包含小写英文字母且非空的字符串SS。在通过执行任意次数的左移和任意次数的右移操作可能得到的所有字符串中,找出字典序最小的字符串和字典序最大的字符串。

什么是字典序?

简单来说,字典序就是字典中单词排列的顺序。更正式地讲,以下是一个确定不同字符串SSTT之间的字典序的算法。

以下,令SiS_i表示字符串SS的第ii个字符。另外,若SS字典序小于TT,我们记作S<TS \lt T;若SS字典序大于TT,我们记作S>TS \gt T

  1. 计算SSTT长度中的较小值并赋给LL。对于每个i=1,2,,Li=1,2,\dots,L,检查SiS_iTiT_i是否相同。
  2. 如果存在某个ii使得SiTiS_i \neq T_i,取其中最小的这样的ii作为jj。然后比较SjS_jTjT_j。如果SjS_j在字母表顺序上先于TjT_j出现,则判定S<TS \lt T并结束比较;若SjS_j后于TjT_j出现,则判定S>TS \gt T并结束比较。
  3. 若不存在满足SiTiS_i \neq T_iii,则比较SSTT的长度。若SSTT短,则判定S<TS \lt T并结束比较;若SSTT长,则判定S>TS \gt T并结束比较。

以上为通义千问 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 SS 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 SS, 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 SS and TT.

Below, let SiS_i denote the ii-th character of SS. Also, if SS is lexicographically smaller than TT, we will denote that fact as S<TS \lt T; if SS is lexicographically larger than TT, we will denote that fact as S>TS \gt T.

  1. Let LL be the smaller of the lengths of SS and TT. For each i=1,2,,Li=1,2,\dots,L, we check whether SiS_i and TiT_i are the same.
  2. If there is an ii such that SiTiS_i \neq T_i, let jj be the smallest such ii. Then, we compare SjS_j and TjT_j. If SjS_j comes earlier than TjT_j in alphabetical order, we determine that S<TS \lt T and quit; if SjS_j comes later than TjT_j, we determine that S>TS \gt T and quit.
  3. If there is no ii such that SiTiS_i \neq T_i, we compare the lengths of SS and TT. If SS is shorter than TT, we determine that S<TS \lt T and quit; if SS is longer than TT, we determine that S>TS \gt T and quit.

Constraints

  • SS consists of lowercase English letters.
  • The length of SS is between 11 and 10001000 (inclusive).

Input

Input is given from Standard Input in the following format:

SS

Output

Print two lines. The first line should contain SminS_{\min}, and the second line should contain SmaxS_{\max}. Here, SminS_{\min} and SmaxS_{\max} are respectively the lexicographically smallest and largest strings obtained by performing zero or more left shifts and zero or more right shifts on SS.

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

}