#P7794. Parentheses Matching
Parentheses Matching
Parentheses Matching
Problem Description
Given a string 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 is a string consisting of only parentheses (i.e. "(" and ")"), and is considered balanced if and only if:
● is an empty string, or
● there exist two balanced parenthesis strings and such that , or
● there exists a balanced parenthesis string such that .
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 and of the same length , we say is smaller than in lexicographical order if and only if there exists some integer such that:
● , and
● the first characters of and that of are exactly the same, and
● the -th character of is smaller than that of .
For instance, "()(())" is smaller than "()()()", and in this case, .
Input
There are several test cases. The first line contains an integer (), denoting the number of test cases. Then follow all the test cases. For each test case, the only line contains a string of length (), denoting the string that consists of only parentheses and asterisk characters. It is guaranteed that the sum of in all test cases is no larger than .
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
2020 Multi-University Training Contest 3