String Shifting AtCoder - abc223_b

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.

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

输入:

string s

输出:

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

样例1:

输入输出
aaba
aaab
baaa

样例2:

输入输出
zz

样例3:

输入输出
abracadabra
aabracadabr
racadabraab

题目大意:

        题目给出一个小写字母组成的字符串,可以将字符串中字母向左移(第一位移到最右),或者将字母向右移,(最后一位移到最左),然后求经过任意次操作后字典序最小和最大的字符串。

思路:

因为字符串长度为1e3,所以如果把字符串左移和右移的每种可能都枚举出来,时间复杂度1e6,再比较字典序大小

#include<bits/stdc++.h>
using namespace std;
char s[1005];
char mini[1005] = { "z" };//初始化字典序最小和最大的字符串
char maxi[1005] = { "a" };
int main()
{
	cin >> s;
	char temp;
	if (strlen(s) == 1)//长度为1的字符串无需判断
		cout << s << endl << s;

	else
	{
        if (strcmp(s, mini) < 0)
			strcpy(mini, s);
		if (strcmp(s, maxi) > 0)
			strcpy(maxi, s);//先比较初始的字符串的大小
		for (int i = 0; i < strlen(s) - 1; i++)
		{//遍历字符串中每一个字母
			for (int j = 0; j < strlen(s) - 1; j++)
			{//将每一个字母与他右边的字母交换位置,得到经过任意次旋转可能的字符串
				temp = s[j];
				s[j] = s[j + 1];
				s[j + 1] = temp;
			}
			if (strcmp(s, mini) < 0)
				strcpy(mini, s);
			if (strcmp(s, maxi) > 0)
				strcpy(maxi, s);
		}
		
		cout << mini << endl << maxi;
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timidcatt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值