A. Reverse a Substring

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of n lowercase Latin letters.

Let’s define a substring as a contiguous subsegment of a string. For example, “acab” is a substring of “abacaba” (it starts in position 3 and ends in position 6), but “aa” or “d” aren’t substrings of this string. So the substring of the string s from position l to position r is s[l;r]=slsl+1…sr.

You have to choose exactly one of the substrings of the given string and reverse it (i. e. make s[l;r]=srsr−1…sl) to obtain a string that is less lexicographically. Note that it is not necessary to obtain the minimum possible string.

If it is impossible to reverse some substring of the given string to obtain a string that is less, print “NO”. Otherwise print “YES” and any suitable substring.

String x is lexicographically less than string y, if either x is a prefix of y (and x≠y), or there exists such i (1≤i≤min(|x|,|y|)), that xi<yi, and for any j (1≤j<i) xj=yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​.

Input
The first line of the input contains one integer n (2≤n≤3⋅105) — the length of s.

The second line of the input contains the string s of length n consisting only of lowercase Latin letters.

Output
If it is impossible to reverse some substring of the given string to obtain a string which is lexicographically less, print “NO”. Otherwise print “YES” and two indices l and r (1≤l<r≤n) denoting the substring you have to reverse. If there are multiple answers, you can print any.

Examples
inputCopy
7
abacaba
outputCopy
YES
2 5
inputCopy
6
aabcfg
outputCopy
NO
Note
In the first testcase the resulting string is “aacabba”.
暴力找一个比前面小的反过来就行

#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#define ll long long
#define dd double
using namespace std;

int main() {
	ll n;
	while (cin >> n) {
		string s;
		cin >> s;
		ll x = 0;
		ll y = 0;
		ll flag = 0;
		for (ll i = 0; i < s.size()-1; i++) {
			if (s[i] > s[i + 1]) {
				x = i + 1;
				y = i + 2;
				flag = 1;
				break;
			}
		}
		if (flag == 0) {
			cout << "NO" << endl;
		}
		else {
			cout << "YES" << endl << x << " " << y << endl;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值