CF1807C.Find and Replace

普及/提高-

USACO

通过率:0%

AC君温馨提醒

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

题目描述

You are given a string ss consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with 0\texttt{0} or replace all occurrences of this character with 1\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 abacaba\texttt{abacaba} . You can perform the following moves:

  • Replace a\texttt{a} with 0\texttt{0} . Now the string is 0b0c0b0\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}}\texttt{c}\color{red}{\texttt{0}}\texttt{b}\color{red}{\texttt{0}} .
  • Replace b\texttt{b} with 1\texttt{1} . Now the string is 010c010{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}}\texttt{c}{\texttt{0}}\color{red}{\texttt{1}}{\texttt{0}} .
  • Replace c\texttt{c} with 1\texttt{1} . Now the string is 0101010{\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 0\texttt{0} s and 1\texttt{1} s such that no two adjacent bits are equal. For example, 01010101\texttt{01010101} , 101\texttt{101} , 1\texttt{1} are alternating binary strings, but 0110\texttt{0110} , 0a0a0\texttt{0a0a0} , 10100\texttt{10100} are not.

输入格式

The input consists of multiple test cases. The first line contains an integer tt ( 1t1001 \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 nn ( 1n20001 \leq n \leq 2000 ) — the length of the string ss .

The second line of each test case contains a string consisting of nn lowercase Latin characters — the string ss .

输出格式

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).

输入输出样例

  • 输入#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 00\texttt{00} and 11\texttt{11} , neither of which are alternating.

In the third test case, you can make 1\texttt{1} , which is an alternating binary string.

首页