A14530 | Balanced Substring
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a string $s$ , consisting of $n$ letters, each letter is either 'a' or 'b'. The letters in the string are numbered from $1$ to $n$ .
$s[l; r]$ is a continuous substring of letters from index $l$ to $r$ of the string inclusive.
A string is called balanced if the number of letters 'a' in it is equal to the number of letters 'b'. For example, strings "baba" and "aabbab" are balanced and strings "aaab" and "b" are not.
Find any non-empty balanced substring $s[l; r]$ of string $s$ . Print its $l$ and $r$ ( $1 \le l \le r \le n$ ). If there is no such substring, then print $-1$ $-1$ .
$s[l; r]$ is a continuous substring of letters from index $l$ to $r$ of the string inclusive.
A string is called balanced if the number of letters 'a' in it is equal to the number of letters 'b'. For example, strings "baba" and "aabbab" are balanced and strings "aaab" and "b" are not.
Find any non-empty balanced substring $s[l; r]$ of string $s$ . Print its $l$ and $r$ ( $1 \le l \le r \le n$ ). If there is no such substring, then print $-1$ $-1$ .
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 1000$ ) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of the testcase contains a single integer $n$ ( $1 \le n \le 50$ ) — the length of the string.
The second line of the testcase contains a string $s$ , consisting of $n$ letters, each letter is either 'a' or 'b'.
Then the descriptions of $t$ testcases follow.
The first line of the testcase contains a single integer $n$ ( $1 \le n \le 50$ ) — the length of the string.
The second line of the testcase contains a string $s$ , consisting of $n$ letters, each letter is either 'a' or 'b'.
输出格式
For each testcase print two integers. If there exists a non-empty balanced substring $s[l; r]$ , then print $l$ $r$ ( $1 \le l \le r \le n$ ). Otherwise, print $-1$ $-1$ .
输入输出样例
输入 #1
4 1 a 6 abbaba 6 abbaba 9 babbabbaa
输出 #1
-1 -1 1 6 3 6 2 5
In the first testcase there are no non-empty balanced subtrings.
In the second and third testcases there are multiple balanced substrings, including the entire string "abbaba" and substring "baba".
In the second and third testcases there are multiple balanced substrings, including the entire string "abbaba" and substring "baba".
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted