题目描述
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters + and 1 into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not. Let's call a regular bracket sequence "RBS".
You are given a sequence ss of nn characters (, ), and/or ?. There is exactly one character ( and exactly one character ) in this sequence.
You have to replace every character ? with either ) or ( (different characters ? can be replaced with different brackets). You cannot reorder the characters, remove them, insert other characters, and each ? must be replaced.
Determine if it is possible to obtain an RBS after these replacements.
输入格式
The first line contains one integer tt ( 1 \le t \le 10001≤t≤1000 ) — the number of test cases.
Each test case consists of one line containing ss ( 2 \le |s| \le 1002≤∣s∣≤100 ) — a sequence of characters (, ), and/or ?. There is exactly one character ( and exactly one character ) in this sequence.
输出格式
For each test case, print YES if it is possible to obtain a regular bracket sequence, or NO otherwise}.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
题意翻译
多测。
给定一个包含 (
,)
,?
三种字符的字符串,并且保证只有一个 (
和一个 )
。
其中你可以将 ?
替换成 (
or )
。
问是否存在一种替换方案,使得替换后的串左右括号匹配。
注意 ?
必须得替换掉,最后留下的必须是个括号串。
存在输出一行 YES
,否则输出一行 NO
。
输入输出样例
输入 #1复制
5 () (?) (??) ??() )?(?
输出 #1复制
YES NO YES YES NO
说明/提示
In the first test case, the sequence is already an RBS.
In the third test case, you can obtain an RBS as follows: ()() or (()).
In the fourth test case, you can obtain an RBS as follows: ()().
#include<stdio.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int len=s.length();
if(len%2!=0||s[0]==')'||s[len-1]=='(')
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
}
}
return 0;
}