Wildcard Parentheses String
InterviewTime limit1sMemory limit1024 MB
Given a string of '(', ')', '?', and '*', decide whether the '?' and '*' can be filled in to form a correct bracket string.
Problem
A string made of ( and ) is a correct bracket string under these rules.
- If is the empty string, is a correct bracket string.
- If is a correct bracket string, then
()is also a correct bracket string. - If and are correct bracket strings, then the concatenation is also a correct bracket string.
- A string that cannot be built with the three rules above is not a correct bracket string.
For example, (())() and ()() are correct bracket strings, while )( and ())( are not.
You are given a string made of (, ), ?, and *. Replace each ? with ( or ), and replace each * with a string of length 0 or more made of ( and ). Can become a correct bracket string?
Input
The first line contains the number of test cases .
Each test case is one line containing a string of length at least 1 and at most . The total length of all strings does not exceed .
Output
For each test case, print YES on its own line if can be turned into a correct bracket string by the replacements above. Otherwise, print NO.