CF1494A.ABC String

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a string aa, consisting of nn characters, nn is even. For each ii from 11 to nn aia_i is one of 'A', 'B' or 'C'.

A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.

You want to find a string bb that consists of nn characters such that:

  • bb is a regular bracket sequence;
  • if for some ii and jj (1≤i,j≤n1 \le i, j \le n) ai=aja_i=a_j, then bi=bjb_i=b_j.

In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.

Your task is to determine if such a string bb exists.

给你一个长度为 nn 的字符串 aa,其中 nn 为偶数。对每个从 11 到 nn 的 ii,aia_i 是 'A'、'B' 或 'C' 中的一个字符。

括号序列是指仅由字符 '(' 和 ')' 组成的字符串。合法括号序列(regular bracket sequence)是指可以通过在原序列的字符之间插入字符 "1" 和 "+",从而将其转换为一个合法算术表达式的括号序列。例如,括号序列 "()()" 和 "(())" 是合法的(对应表达式分别为 "(1)+(1)" 和 "((1+1)+1)"),而 ")("、"(" 和 ")" 则不是。

你需要找到一个由 nn 个字符组成的字符串 bb,满足以下条件:

  • bb 是一个合法括号序列;
  • 若对某些 ii 和 jj(1≤i,j≤n1 \le i, j \le n)有 ai=aja_i = a_j,则必有 bi=bjb_i = b_j。

换言之,你要将所有 'A' 替换为同一种括号(即全为 '(' 或全为 ')'),所有 'B' 替换为同一种括号,所有 'C' 也替换为同一种括号。

你的任务是判断是否存在这样的字符串 bb。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of testcases.

Then the descriptions of tt testcases follow.

The only line of each testcase contains a string aa. aa consists only of uppercase letters 'A', 'B' and 'C'. Let nn be the length of aa. It is guaranteed that nn is even and 2≤n≤502 \le n \le 50.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

接下来是 tt 个测试用例的描述。

每个测试用例仅有一行,包含一个字符串 aa。aa 仅由大写字母 'A'、'B' 和 'C' 组成。设 nn 为 aa 的长度。保证 nn 为偶数,且 2≤n≤502 \le n \le 50。

输出格式

For each testcase print "YES" if there exists such a string bb that:

  • bb is a regular bracket sequence;
  • if for some ii and jj (1≤i,j≤n1 \le i, j \le n) ai=aja_i=a_j, then bi=bjb_i=b_j.

Otherwise, print "NO".

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

对于每个测试用例,如果存在满足以下条件的字符串 bb,则输出 "YES":

  • bb 是一个合法的括号序列;
  • 若对某些 ii 和 jj(1≤i,j≤n1 \le i, j \le n)有 ai=aja_i = a_j,则必有 bi=bjb_i = b_j。

否则,输出 "NO"。

你可以以任意大小写形式输出每个字母(例如,字符串 yEs、yes、Yes 和 YES 均被视为肯定回答)。

输入输出样例

  • 输入#1

    4
    AABBAC
    CACA
    BBBBAC
    ABCA

    输出#1

    YES
    YES
    NO
    NO

说明/提示

In the first testcase one of the possible strings bb is "(())()".

In the second testcase one of the possible strings bb is "()()".

在第一个测试用例中,一个可能的字符串 bb 是 "(())()"。

在第二个测试用例中,一个可能的字符串 bb 是 "()()"。

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

首页