CF1494A

A. ABC String

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string aa, consisting of nn characters, nn is even. For each ii from 11 to nn aiai 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≤i,j≤n) ai=ajai=aj, then bi=bjbi=bj.

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.

Input

The first line contains a single integer tt (1≤t≤10001≤t≤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≤n≤50.

Output

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≤i,j≤n) ai=ajai=aj, then bi=bjbi=bj.

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

Example

input

Copy

4
AABBAC
CACA
BBBBAC
ABCA

output

Copy

YES
YES
NO
NO

Note

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

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


在https://www.bilibili.com/video/BV1fU4y1H7pu?t=175大佬的视频下,又扩展了思路

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctype.h>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <sstream>
#define ll unsigned long long
#define re return

using namespace std;

int T;
string s;
int t[55];

bool check(int l){ //检查是否匹配
    int cnt = 0;
    for (int i = 0; i < l; i++){
        if(!t[i])
            cnt++; //如果出现左括号(0)就加1
        else{ //如果是右括号
            if(!cnt) // 此时的左括号个数为0的话,加右括号是不合法的
                return false;
            else{
                cnt--;
            }
        }
    }
    if (cnt != 0) // 如果最后的左括号没有完全匹配的话,也是不合法的
        return 0;
    return 1;
}

int main(){
    cin >> T;
    while(T--){
        cin >> s;
        int l = s.size();
        int flag = 0;
        // 因为数据的复杂度不高,所以可以枚举每个可能的情况来进行判断,时间复杂度也不会太高
        for (int a = 0; a <= 1; a++){ // 0代表左括号,1代表右括号
            for (int b = 0; b <= 1; b++){
                for (int c = 0; c <= 1; c++){
                    
                    for (int i = 0; i < l; i++){
                        if(s[i] == 'A')
                            t[i] = a;
                        if(s[i] == 'B')
                            t[i] = b;
                        if(s[i] == 'C')
                            t[i] = c;
                    }
                    flag |= check(l); // 异或处理后,只要出现了1,
                }
            }
        }
        if(flag)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    re 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值