2023 ICPC 亚洲区域赛 济南站 A. Many Many Heads

A. Many Many Heads

time limit per test: 1 second

memory limit per test: 1024 megabytes

Multi-Heads Cup, or MHC for short, is a worldwide programming contest made for the participants with many many heads. The Chief Judge of the competition, Little Cyan Fish, is considering to design an identification number for each participant.

"And that's it," Little Cyan Fish thought, "Let's use some bracket sequences!" He assigned a unique balanced bracket sequence to each participant, with two kinds of the brackets - the round brackets (also known as parentheses), and the square brackets. To make sure you understand the concepts of balanced bracket sequence, Little Cyan Fish prepared a formal definition of the balanced bracket sequence:

ε (an empty string) is a balanced bracket sequence.If A is a balacned bracket sequence, then (A) and [A] are both balanced bracket sequences.If A and B are balanced bracket sequences, then AB is also a balanced bracket sequence.

For example, "()", "[()]" and "[()]()" are balanced bracket sequences, but ")(", "[(])" and "[)" are not.

For our multi-headed participants, memorizing a bracket sequence is not a difficult task. However, the challenge lies in their unique ability: because they have too much heads, they can't discern the direction of each bracket! Consequently, compared to the original balanced bracket sequence, they might recall the sequence with some brackets changing their directions. For example, the bracket sequence "[()]()" might be memorized as "]))]))" or "]()]))". Fortunately, the type of brackets are guaranteed to remain unchanged.

On the contest day, as Little Cyan Fish receives each participant's bracket sequence, a question arises: Can the original bracket sequence be deduced uniquely? In other words, Little Cyan Fish needs to determine if the provided bracket sequence maps to exactly one balanced bracket sequence.

Please, help Little Cyan Fish to finish this task, so our multi-heads friends will be able to participate in the contest!

Input

There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:

The first line contains a string S consisting of '(', ')', '[', and ']' (1≤|S|≤10^{5}), indicating the bracket sequence.

It is guaranteed that:

The sum of |S| over all test cases does not exceed 106.

Each bracket sequence is obtained by changing the directions of some brackets in a balanced bracket sequence.

Output

For each test case:

If the provided bracket sequence maps to more than one balanced bracket sequence, output a single line No.

Otherwise, output a single line Yes.

Example

input

6

))

((()

[()]

()[()]()

([()])

([])([])

output

Yes

No

Yes

No

Yes

No

Note

In the first test case, the bracket sequence maps to exactly one balanced bracket sequence: (). So the answer is Yes.

In the second test case, the bracket sequence corresponds to two distinct balanced bracket sequences: (()) and ()(). So the answer is No.

In the third test case, the bracket sequence maps to exactly one balanced bracket sequence: [()]. So the answer is Yes.

In the fourth test case, the bracket sequence corresponds to two distinct balanced bracket sequences: (([()])) and ()[()](). So the answer is No.

In the fifth test case, the bracket sequence maps to exactly one balanced bracket sequence: ([()]). So the answer is Yes.

In the sixth test case, the bracket sequence corresponds to three distinct balanced bracket sequences: ([])([]), ([]()[]) and ([[()]]). So the answer is No.

【思路分析】

数学+思维。本题主要考察思维。首先做数据预处理,将相同类型括号转换为0或1,原字符串转化为01序列。基于原序列,考虑以下几点:各元素相同的连续子序列长度大于3时,形如(((A)))可化为(()A()),此时方案数不唯一。各元素相同的连续子序列长度为2时,形如((A))转化成()A(),此时序列合法且方案数唯一。但该种情况的序列个数大于2时,会出现(([[A]]))、(([]A[]))情况,方案数不唯一。其余情况较简单,此处不做证明。对该两种情况判断即可。

#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <cstring>
#include <iomanip>
#include <set>
#include <queue>

#define i64 long long

using namespace std;

typedef pair<i64,i64> pii;

void solve() {
    i64 cnt = 0;
    string s;
    cin>>s;
    if (s.size()==2) {
        cout<<"Yes"<<endl;
        return;
    }
    for (auto &item: s) {
        if (item=='['||item==']') item = '1';
        else item = '0';
    }
    for (int i = 1; i < s.size(); ++i) {
        if (s[i]==s[i-1]) ++cnt;
        if (cnt>2) {
            cout<<"No"<<endl;
            return;
        }
    }
    cout<<"Yes"<<endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值