#P16308. [ICPC 2023 Jinan R] 很多很多头
[ICPC 2023 Jinan R] 很多很多头
Problem Description
Many Heads Cup, abbreviated as MHC, is a world-class programming contest for contestants who have . The chief judge of this contest, Little Mackerel, is considering designing an ID number for each contestant.
“Then, let’s use this,” Little Mackerel thought, “let’s use some bracket sequences!” He assigns each contestant a unique balanced bracket sequence. Each sequence contains two types of brackets: parentheses (also called round brackets), and square brackets. To make sure you understand what a balanced bracket sequence is, Little Mackerel prepared the following formal definition:
- (an empty string) is a balanced bracket sequence.
- If is a balanced bracket sequence, then and are also balanced bracket sequences.
- If and are balanced bracket sequences, then is also a balanced bracket sequence.
For example, , , and are balanced bracket sequences, but , , and are not.
For our contestants with many heads, memorizing bracket sequences is not difficult. However, the problem lies in their unique ability: because they have too many heads, they cannot tell the direction of a bracket. As a result, compared to the original balanced bracket sequence, the sequence in their memory may have some brackets flipped. For example, the bracket sequence might be remembered as or . Fortunately, the bracket type still remains unchanged.
:::align{center}
:::
On the contest day, after Little Mackerel received each contestant’s bracket sequence, a question arose: can the original bracket sequence be uniquely determined? In other words, Little Mackerel needs to determine whether the given bracket sequence corresponds to exactly one balanced bracket sequence.
Please help Little Mackerel complete this task, so that our many-headed friends can participate in the contest.
Input Format
There are multiple test cases. The first line contains an integer denoting the number of test cases. For each test case:
The first line contains a string consisting of , , , and (), representing the bracket sequence.
It is guaranteed that:
- The sum of over all test cases does not exceed .
- Each bracket sequence is obtained by flipping the direction of some brackets in a certain balanced bracket sequence.
Output Format
For each test case:
- If the given bracket sequence can correspond to more than one balanced bracket sequence, output one line .
- Otherwise, output one line .
6
))
((()
[()]
()[()]()
([()])
([])([])
Yes
No
Yes
No
Yes
No
Hint
For the first sample, the bracket sequence corresponds to exactly one balanced bracket sequence: . So the answer is .
For the second sample, the bracket sequence can correspond to two different balanced bracket sequences: and . So the answer is .
For the third sample, the bracket sequence corresponds to exactly one balanced bracket sequence: . So the answer is .
For the fourth sample, the bracket sequence can correspond to two different balanced bracket sequences: and . So the answer is .
For the fifth sample, the bracket sequence corresponds to exactly one balanced bracket sequence: . So the answer is .
For the sixth sample, the bracket sequence can correspond to three different balanced bracket sequences: , , and . So the answer is .
Translated by ChatGPT 5