Educational Codeforces Round 63 (Rated for Div. 2)A. Reverse a Substring【水题】

A. Reverse a Substring

You are given a string ss consisting of nn 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 33and ends in position 66), but "aa" or "d" aren't substrings of this string. So the substring of the string ss from position ll to position rr is s[l;r]=slsl+1…srs[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…sls[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 anysuitable substring.

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

Input

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

The second line of the input contains the string ss of length nn 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 ll and rr (1≤l<r≤n1≤l<r≤n) denoting the substring you have to reverse. If there are multiple answers, you can print any.

思路:就是给你一个字符串让你判断是否存在一个子串反转后字典序变小,刚开始想的是两层循环,当时脑子短路了,然后反应过来直接判断连续两个字符的大小关系就行了。

#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;
string s;


int main()
{
    int n;
    scanf("%d", &n);
    cin >> s;
    bool flag = false;
    int l, r;
    for(int i = 0; i < n - 1; ++i) //注意不要超出范围
    {
        if(s[i + 1] < s[i])
        {
            flag = true;
            r = i + 2;
            l = i + 1;
            break;
        }
    }
    if(flag)
        printf("YES\n%d %d\n", l, r);
    else
        printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/89480786