A.Reverse a Substring----贪心思维 Educational Codeforces Round 63 (Rated for Div. 2)

Reverse a Substring

time limit per test 2 seconds
memory limit per test 256 megabytes

题目链接https://codeforces.com/problemset/problem/1155/A
在这里插入图片描述

Examples

7
abacaba
YES
2 5

6
aabcfg
NO

Note
In the first testcase the resulting string is “aacabba”.


题目大意:问你是否能颠倒一段顺序使得字符串的字典序能够减小并输出颠倒的头和尾的位置。。

就是一个简单的贪心,如果不能够操作,则字符串按照升序排列。。。

接下来我们从头到尾搜索一遍,建立vector 数组来保存已经搜索到的每个字母的位置:

g[s[i]-'a'].push_back(i);

我们每次检查,s[i]之前是否存在比它字典序大的字母如果有则存下来:

for (int j=s[i]-'a'+1; j<26; j++) {
	if (g[j].size()) {
		posma=g[j][0];
		posmi=i;
		flag=1;
		break;
	}
}

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;
const int mac=3e5+10;
char s[mac];
vector<int>g[40];
int main(){
	int n;
	cin>>n;
	scanf ("%s",s);
	int len=strlen(s);
	int mark=0,posma=0,posmi=0;
	for (int i=1; i<len; i++){
		if (s[i]<s[i-1]){
			mark=1;break;
		}
	}
	if (!mark){
		printf ("NO\n");
	}
	else {
		int flag=0;
		g[s[0]-'a'].push_back(0);
		for (int i=1; i<len; i++){
			g[s[i]-'a'].push_back(i);
			for (int j=s[i]-'a'+1; j<26; j++){
				if (g[j].size()){
					posma=g[j][0];
					posmi=i;flag=1;break;
				}
			}
			if (flag) break;
		}
		printf ("YES\n");
		printf ("%d %d\n",posma+1,posmi+1);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值