A12471 | Reverse a Substring
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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] = 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] = 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 $x$ is lexicographically less than string $y$ , if either $x$ is a prefix of $y$ (and $x \ne y$ ), or there exists such $i$ ( $1 \le i \le min(|x|, |y|)$ ), that $x_i < y_i$ , and for any $j$ ( $1 \le j < i$ ) $x_j = y_j$ . Here $|a|$ denotes the length of the string $a$ . The lexicographic comparison of strings is implemented by operator < in modern programming languages.
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] = 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] = 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 $x$ is lexicographically less than string $y$ , if either $x$ is a prefix of $y$ (and $x \ne y$ ), or there exists such $i$ ( $1 \le i \le min(|x|, |y|)$ ), that $x_i < y_i$ , and for any $j$ ( $1 \le j < i$ ) $x_j = y_j$ . Here $|a|$ denotes the length of the string $a$ . The lexicographic comparison of strings is implemented by operator < in modern programming languages.
输入格式
The first line of the input contains one integer $n$ ( $2 \le n \le 3 \cdot 10^5$ ) — the length of $s$ .
The second line of the input contains the string $s$ of length $n$ consisting only of lowercase Latin letters.
The second line of the input contains the string $s$ of length $n$ consisting only of lowercase Latin letters.
输出格式
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 \le l < r \le n$ ) denoting the substring you have to reverse. If there are multiple answers, you can print any.
输入输出样例
输入 #1
7 abacaba
输出 #1
YES 2 5
输入 #2
6 aabcfg
输出 #2
NO
In the first testcase the resulting string is "aacabba".
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted