CF1155A.Reverse a Substring

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 33 and 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] = s_l s_{l + 1} \dots s_r.

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] = s_r s_{r - 1} \dots s_l) 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 xx is lexicographically less than string yy, if either xx is a prefix of yy (and x≠yx \ne y), or there exists such ii (1≤i≤min(∣x∣,∣y∣)1 \le i \le min(|x|, |y|)), that xi<yix_i \lt y_i, and for any jj (1≤j<i1 \le j \lt i) xj=yjx_j = y_j. Here ∣a∣|a| denotes the length of the string aa. The lexicographic comparison of strings is implemented by operator < in modern programming languages.

给你一个由 nn 个小写拉丁字母组成的字符串 ss。

我们定义子串为字符串的一个连续子段。例如,“acab” 是 “abacaba” 的一个子串(它起始于位置 33,终止于位置 66),但 “aa” 或 “d” 并不是该字符串的子串。因此,字符串 ss 中从位置 ll 到位置 rr 的子串记为 s[l;r]=slsl+1…srs[l; r] = s_l s_{l + 1} \dots s_r。

你需要恰好选择给定字符串的一个子串,并将其反转(即令 s[l;r]=srsr−1…sls[l; r] = s_r s_{r - 1} \dots s_l),使得所得字符串在字典序上严格小于原字符串。注意:不一定要得到字典序最小的可能字符串。

如果不可能通过反转某个子串来得到字典序更小的字符串,则输出 "NO";否则输出 "YES",并给出任意一个满足条件的子串(即其起始与结束位置)。

字符串 xx 在字典序上小于字符串 yy,当且仅当以下任一条件成立:

  • xx 是 yy 的真前缀(即 x≠yx \ne y,且 yy 以 xx 开头);
  • 或存在某个下标 ii(1≤i≤min⁡(∣x∣,∣y∣)1 \le i \le \min(|x|, |y|)),使得 xi<yix_i \lt y_i,且对所有 jj(1≤j<i1 \le j \lt i)均有 xj=yjx_j = y_j。
    其中 ∣a∣|a| 表示字符串 aa 的长度。现代编程语言中,字符串的字典序比较即由运算符 < 实现。

输入格式

The first line of the input contains one integer nn (2≤n≤3⋅1052 \le n \le 3 \cdot 10^5) — the length of ss.

The second line of the input contains the string ss of length nn consisting only of lowercase Latin letters.

输入的第一行包含一个整数 nn(2≤n≤3⋅1052 \le n \le 3 \cdot 10^5)—— 字符串 ss 的长度。

输入的第二行包含一个长度为 nn 的字符串 ss,仅由小写拉丁字母组成。

输出格式

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 \le l \lt r \le n) denoting the substring you have to reverse. If there are multiple answers, you can print any.

如果无法通过反转给定字符串的某个子串来得到字典序更小的字符串,则输出 "NO"。否则,输出 "YES" 以及两个下标 ll 和 rr(1≤l<r≤n1 \le l \lt r \le n),表示需要反转的子串。若存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    7
    abacaba

    输出#1

    YES
    2 5
  • 输入#2

    6
    aabcfg

    输出#2

    NO

说明/提示

In the first testcase the resulting string is "aacabba".

在第一个测试用例中,得到的字符串为 "aacabba"。

输入解题思路,AI测评打分。不知道怎么写?

首页