A15807 | Find and Replace
来源USACO / 2023
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a string $s$ consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with $\texttt{0}$ or replace all occurrences of this character with $\texttt{1}$ .
Is it possible to perform some number of moves so that the resulting string is an alternating binary string $^{\dagger}$ ?
For example, consider the string $\texttt{abacaba}$ . You can perform the following moves:
- Replace $\texttt{a}$ with $\texttt{0}$ . Now the string is $\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}\texttt{c}\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}$ .
- Replace $\texttt{b}$ with $\texttt{1}$ . Now the string is ${\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}\texttt{c}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}$ .
- Replace $\texttt{c}$ with $\texttt{1}$ . Now the string is ${\texttt{0}}{\texttt{1}}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}{\texttt{1}}{\texttt{0}}$ . This is an alternating binary string.
$^{\dagger}$ An alternating binary string is a string of $\texttt{0}$ s and $\texttt{1}$ s such that no two adjacent bits are equal. For example, $\texttt{01010101}$ , $\texttt{101}$ , $\texttt{1}$ are alternating binary strings, but $\texttt{0110}$ , $\texttt{0a0a0}$ , $\texttt{10100}$ are not.
Is it possible to perform some number of moves so that the resulting string is an alternating binary string $^{\dagger}$ ?
For example, consider the string $\texttt{abacaba}$ . You can perform the following moves:
- Replace $\texttt{a}$ with $\texttt{0}$ . Now the string is $\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}\texttt{c}\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}$ .
- Replace $\texttt{b}$ with $\texttt{1}$ . Now the string is ${\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}\texttt{c}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}$ .
- Replace $\texttt{c}$ with $\texttt{1}$ . Now the string is ${\texttt{0}}{\texttt{1}}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}{\texttt{1}}{\texttt{0}}$ . This is an alternating binary string.
$^{\dagger}$ An alternating binary string is a string of $\texttt{0}$ s and $\texttt{1}$ s such that no two adjacent bits are equal. For example, $\texttt{01010101}$ , $\texttt{101}$ , $\texttt{1}$ are alternating binary strings, but $\texttt{0110}$ , $\texttt{0a0a0}$ , $\texttt{10100}$ are not.
输入格式
The input consists of multiple test cases. The first line contains an integer $t$ ( $1 \leq t \leq 100$ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $n$ ( $1 \leq n \leq 2000$ ) — the length of the string $s$ .
The second line of each test case contains a string consisting of $n$ lowercase Latin characters — the string $s$ .
The first line of each test case contains an integer $n$ ( $1 \leq n \leq 2000$ ) — the length of the string $s$ .
The second line of each test case contains a string consisting of $n$ lowercase Latin characters — the string $s$ .
输出格式
For each test case, output "YES" (without quotes) if you can make the string into an alternating binary string, and "NO" (without quotes) otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
输入输出样例
输入 #1
8 7 abacaba 2 aa 1 y 4 bkpt 6 ninfia 6 banana 10 codeforces 8 testcase
输出 #1
YES NO YES YES NO YES NO NO
The first test case is explained in the statement.
In the second test case, the only possible binary strings you can make are $\texttt{00}$ and $\texttt{11}$ , neither of which are alternating.
In the third test case, you can make $\texttt{1}$ , which is an alternating binary string.
In the second test case, the only possible binary strings you can make are $\texttt{00}$ and $\texttt{11}$ , neither of which are alternating.
In the third test case, you can make $\texttt{1}$ , which is an alternating binary string.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?