C. Recover an RBS(括号处理)

原题链接:

A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) 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)");
  • bracket sequences ")(", "(" and ")" are not.

There was an RBS. Some brackets have been replaced with question marks. Is it true that there is a unique way to replace question marks with brackets, so that the resulting sequence is an RBS?

Input

The first line contains a single integer tt (1≤t≤5⋅1041≤t≤5⋅104) — the number of testcases.

The only line of each testcase contains an RBS with some brackets replaced with question marks. Each character is either '(', ')' or '?'. At least one RBS can be recovered from the given sequence.

The total length of the sequences over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print "YES" if the way to replace question marks with brackets, so that the resulting sequence is an RBS, is unique. If there is more than one way, then print "NO".

Example

input

Copy

5
(?))
??????
()
??
?(?)()?)

output

Copy

YES
NO
YES
YES
NO

Note

In the first testcase, the only possible original RBS is "(())".

In the second testcase, there are multiple ways to recover an RBS.

In the third and the fourth testcases, the only possible original RBS is "()".

In the fifth testcase, the original RBS can be either "((()()))" or "(())()()".

思路:

1,遇到过好多次序号配对的题,有queue处理的,有数记载的(‘(’为1,‘)‘为-1)

2,(为1,)为-1,?单独记载,

当cnt《0时,必有?与之配对,now--,cnt++;

当cnt==0,now==1时,(列如()?),?只能是‘(‘,因为题中保证有解,)的话,直接没解,

3,最后看是否完全为一配对

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vec vector<int>
#define rep(i,l,r) for(int i=l;i<=r;++i)
#pragma GCC optimize(2)//
#pragma GCC optimize(3,"Ofast","inline")//
const int maxj=2e5+10,mod=1e9+7,nn=33554432,inf=0x7f7f7f7f7f7f7f;
int ksm(int a,int b){//快速幂
    int ans=1;
    while(b){
        if(b&1)ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
int log2(int a){
    return floor(log(a)/log(2));
}
int lower_bit(int x){
    return x&(-x);
}
// bool check(int x){//判断回文数
//     int y=x,t=0;
//     while(y){
//         t=t*10+y%10;
//         y/=10;
//     }return x==t;
// }
// bool cmp1(int a,int b){//从大到小
//     return a>b;
// }
// bool cmpp(pair<char,int>a,pair<char,int>b){
//     return a.second<b.second;
// }
// int pai(int x){//全排列
//     if(x==0||x==1)return 1;
//     return x*pai(x-1)%mod;
// }
// int e[maxj],nex[maxj],head[maxj],cnt=1;
// void add(int u,int v){//链式前向星
//     e[cnt]=v;
//     nex[cnt]=head[u];
//     head[u]=cnt++;
// }
// bool cmp(node a,node b){//结构体排序
//     return a.c>b.c;
// }
void solve(){
    string s;cin>>s;
    int cnt=0,now=0;
    rep(i,0,s.size()-1){//先考虑RBS,去配对,然后才考虑是否是唯一解
        if(s[i]=='(')cnt++;
        else if(s[i]==')'){
            cnt--;
            while(cnt<0){
                cnt++,now--;//(和?配对
            }
        }else now++;
        if(cnt==0&&now==1)now--,cnt++;//题目中保证有合法解
    }if(abs(cnt)==now)cout<<"YES"<<'\n';
    else cout<<"NO"<<'\n';
}
signed main(){
    ios::sync_with_stdio(0);//能不用c语言的输入输出,尽量不要用
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);//a为add
#endif
    int t;
    cin>>t;
    // t=1;
    while(t--)solve();
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值