#3510. String Shifting

0
    ID: 3510 传统题 2000ms 1024MiB 尝试: 0 已通过: 0 难度: 1 上传者: 标签>AtCoderabc223BGESP 3级字符串枚举(单层循环)

String Shifting

String Shifting

题目描述

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 abcdeabcde results in bcdeabcdea, and two right shifts on abcdeabcde result in deabcdeabc. 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.

    Let $L$ be the smaller of the lengths of $S$ and $T$. For each $i=1,2,\dots,L$, we check whether $S_i$ and $T_i$ are the same. If there is an $i$ such that $S_i \neq T_i$, let $j$ be the smallest such $i$. Then, we compare $S_j$ and $T_j$. If $S_j$ comes earlier than $T_j$ in alphabetical order, we determine that $S \lt T$ and quit; if $S_j$ comes later than $T_j$, we determine that $S \gt T$ and quit.

    If there is no ii such that Si≠TiS_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.

输入格式

Input is given from Standard Input in the following format: SS

输出格式

Print two lines. The first line should contain Smin⁡S_{\min}, and the second line should contain Smax⁡S_{\max}. Here, Smin⁡S_{\min} and Smax⁡S_{\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.

aaba
aaab
baaa
z
z
z
abracadabra
aabracadabr
racadabraab

提示

SS consists of lowercase English letters. The length of SS is between 11 and 10001000 (inclusive).By performing shifts, we can obtain four strings: aaabaaab, aabaaaba, abaaabaa, baaabaaa. The lexicographically smallest and largest among them are aaabaaab and baaabaaa, respectively.
Any sequence of operations results in zz.

标签: AtCoder|abc223B

来源

AtCoder|abc223B