#P16308. [ICPC 2023 Jinan R] 很多很多头

    ID: 18244 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>模拟字符串2023ICPC济南

[ICPC 2023 Jinan R] 很多很多头

Problem Description

Many Heads Cup, abbreviated as MHC, is a world-class programming contest for contestants who have many many heads\textbf{many many heads}. 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:

  • ε\varepsilon (an empty string) is a balanced bracket sequence.
  • If AA is a balanced bracket sequence, then (A)(A) and [A][A] are also balanced bracket sequences.
  • If AA and BB are balanced bracket sequences, then ABAB is also a balanced bracket sequence.

For example, ()\tt{()}, [()]\tt{[()]}, and [()]()\tt{[()]()} are balanced bracket sequences, but )(\tt{)(}, [(])\tt{[(])}, and [)\tt{[)} 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 [()]()\tt{[()]()} might be remembered as ]))]))\tt{]))]))} or ]()]))\tt{]()]))}. 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 TT denoting the number of test cases. For each test case:

The first line contains a string SS consisting of (\tt{(}, )\tt{)}, [\tt{[}, and ]\tt{]} (1S1051 \leq |S| \leq 10^5), representing the bracket sequence.

It is guaranteed that:

  • The sum of S|S| over all test cases does not exceed 10610^6.
  • 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 No\tt{No}.
  • Otherwise, output one line Yes\tt{Yes}.
6
))
((()
[()]
()[()]()
([()])
([])([])
Yes
No
Yes
No
Yes
No

Hint

For the first sample, the bracket sequence corresponds to exactly one balanced bracket sequence: ()\tt{()}. So the answer is Yes\tt{Yes}.

For the second sample, the bracket sequence can correspond to two different balanced bracket sequences: (())\tt{(())} and ()()\tt{()()}. So the answer is No\tt{No}.

For the third sample, the bracket sequence corresponds to exactly one balanced bracket sequence: [()]\tt{[()]}. So the answer is Yes\tt{Yes}.

For the fourth sample, the bracket sequence can correspond to two different balanced bracket sequences: (([()]))\tt{(([()]))} and ()[()]()\tt{()[()]()}. So the answer is No\tt{No}.

For the fifth sample, the bracket sequence corresponds to exactly one balanced bracket sequence: ([()])\tt{([()])}. So the answer is Yes\tt{Yes}.

For the sixth sample, the bracket sequence can correspond to three different balanced bracket sequences: ([])([])\tt{([])([])}, ([]()[])\tt{([]()[])}, and ([[()]])\tt{([[()]])}. So the answer is No\tt{No}.

Translated by ChatGPT 5