F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Parentheses Matching

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1580    Accepted Submission(s): 668


Problem Description
Given a string $P$ consisting of only parentheses and asterisk characters (i.e. "(", ")" and "*"), you are asked to replace all the asterisk characters in order to get a balanced parenthesis string with the shortest possible length, where you can replace each "*" by one "(", or one ")", or an empty string "".

A parenthesis string $S$ is a string consisting of only parentheses (i.e. "(" and ")"), and is considered balanced if and only if:

    ¡ñ $S$ is an empty string, or
    ¡ñ there exist two balanced parenthesis strings $A$ and $B$ such that $S = A B$, or
    ¡ñ there exists a balanced parenthesis string $C$ such that $S = (C)$.

For instance, "", "()", "(())", "()()", "()(())" are balanced parenthesis strings.

Due to some notorious technical inability, if there are several solutions with the shortest possible length, then you have to report the smallest possible one in lexicographical order.

For every two different strings $A$ and $B$ of the same length $n$, we say $A$ is smaller than $B$ in lexicographical order if and only if there exists some integer $k$ such that:

    ¡ñ $1 \leq k \leq n$, and
    ¡ñ the first $(k - 1)$ characters of $A$ and that of $B$ are exactly the same, and
    ¡ñ the $k$-th character of $A$ is smaller than that of $B$.

For instance, "()(())" is smaller than "()()()", and in this case, $k = 4$.
 

Input
There are several test cases.

The first line contains an integer $T$ ($1 \leq T \leq 10^5$), denoting the number of test cases. Then follow all the test cases.

For each test case, the only line contains a string of length $n$ ($1 \leq n \leq 10^5$), denoting the string $P$ that consists of only parentheses and asterisk characters.

It is guaranteed that the sum of $n$ in all test cases is no larger than $5 \times 10^6$.
 

Output
For each test case, output in one line "No solution!" (without quotes) if no solution exists, or otherwise the smallest possible solution in lexicographical order. Note that the output characters are case-sensitive.
 

Sample Input
5 *))*) *(*)* *)*(* ****** ((***)()((**
 

Sample Output
No solution! () ()() (())()(())
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-24 20:20:25, Gzip enabled