A21228. 移动字符串
填空题
中等
知识点
题目描述
移动字符串
题目描述
你有一个非空字符串 s,仅由小写字母组成。你可以对 s 进行两种操作:
左移:将第一个字符移到最后
右移:将最后一个字符移到最前
例如,字符串 "abcde" 左移一次得到 "bcdea",右移两次得到 "deabc"。
通过进行任意次(包括 0 次)左移或右移,你能得到许多不同的字符串。请找出其中字典序最小和字典序最大的字符串。
输入格式
一个字符串 s
输出格式
第一行:字典序最小的字符串(正序 asc)
第二行:字典序最大的字符串(到序 desc)
输入样例#1
yx输出样例#1
xy
yx输入样例#2
c输出样例#2
c
c说明提示
1≤∣s∣≤10001≤∣s∣≤1000 s仅包含小写英文字母
参考答案
#include<iostream>
int main()
{
std::string s;
std::cin >> s;
std::string min = s;
std::string max = s;
for (unsigned i = 0; i < s.size(); ++i)
{
std::string begin = s.substr(0, i);
std::string end = s.substr(i);
std::string t = end + begin;
if (min > t)
min = t;
if (max < t)
max = t;
}
std::cout << min << "\n";
std::cout << max << "\n";
}
上一题
下一题