Educational Codeforces Round 63 (Rated for Div. 2) A. Reverse a Substring

A. Reverse a Substring

题目链接-A. Reverse a Substring
在这里插入图片描述
在这里插入图片描述
题目大意
就是给你一个字符串让你判断是否存在一个子串反转后字典序变小
解题思路
贪心

  • 只需找到一个字符比它前面的字符字典序小即可
  • 具体操作见代码

附上代码

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#pragma GCC diagnostic error "-std=c++11"
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
string s;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int n;
	cin>>n;
	cin>>s;
	for(int i=1;i<s.length();i++){
		if(s[i]<s[i-1]){
			cout<<"YES"<<endl;
			cout<<i<<" "<<i+1<<endl;
			return 0;
		}
	} 
	cout<<"NO"<<endl;
	return 0;
}

发布了175 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/105370961