#C10337. Balanced Parentheses Filter

    ID: 39531 Type: Default 1000ms 256MiB

Balanced Parentheses Filter

Balanced Parentheses Filter

You are given a list of strings. Each string may contain letters, digits, special characters, and parentheses ( ). A string is said to have balanced parentheses if every opening parenthesis ( has a corresponding closing parenthesis ) and the pairs are properly nested.

Formally, if we define a function \(f\) on each character as:

[ f(c)=\begin{cases} 1, & \text{if } c = '(' \ -1, & \text{if } c = ')' \ 0, & \text{otherwise} \end{cases} ]

Then a string \(S\) is balanced if for every prefix \(S[1\dots i]\), \(\sum_{k=1}^{i}f(S[k]) \ge 0\) and \(\sum_{k=1}^{|S|}f(S[k]) = 0\).

Your task is to filter out and print only those strings from the input that contain balanced parentheses.

inputFormat

The input is read from standard input (stdin) and has the following format:

  1. An integer \(n\) representing the number of strings.
  2. \(n\) lines follow, each containing one string.

outputFormat

Print each string that contains balanced parentheses on a new line in the same order as the input. If no string is balanced, print nothing.

## sample
3
(a+b)
(a+b
(a+b)*(c+d)
(a+b)

(a+b)*(c+d)

</p>