I - Parentheses Matching
https://vjudge.net/contest/386223#problem/I
A parenthesis string SS is a string consisting of only parentheses (i.e. "(" and ")"), and is considered balanced if and only if:
● SS is an empty string, or
● there exist two balanced parenthesis strings AA and BB such that S=ABS=AB, or
● there exists a balanced parenthesis string CC such that S=(C)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 AA and BB of the same length nn, we say AA is smaller than BB in lexicographical order if and only if there exists some integer kk such that:
● 1≤k≤n1≤k≤n, and
● the first (k−1)(k−1) characters of AA and that of BB are exactly the same, and
● the kk-th character of AA is smaller than that of BB.
For instance, "()(())" is smaller than "()()()", and in this case, k=4k=4.
InputThere are several test cases.
The first line contains an integer TT (1≤T≤1051≤T≤105), denoting the number of test cases. Then follow all the test cases.
For each test case, the only line contains a string of length nn (1≤n≤1051≤n≤105), denoting the string PP that consists of only parentheses and asterisk characters.
It is guaranteed that the sum of nn in all test cases is no larger than 5×1065×106.
OutputFor 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! () ()() (())()(())
Sponsor
手写栈 贪心做匹配
参考了https://www.cnblogs.com/lipoicyclic/p/13396109.html
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<ctime> #include<deque> #include<iomanip> #include<list> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include <vector> #include <iterator> #include <utility> #include <sstream> #include <limits> #include <numeric> #include <functional> using namespace std; #define gc getchar() #define mem(a) memset(a,0,sizeof(a)) //#define sort(a,n,int) sort(a,a+n,less<int>()) #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef char ch; typedef double db; const double PI=acos(-1.0); const double eps=1e-6; const int inf=0x3f3f3f3f; //const int maxn=1e5+10; const int maxn=5000005; const int maxm=100+10; const int N=2e5+10; const int mod=1e9+7; bool ima[maxn] = {0}; char S[maxn]; int sum_l[maxn] = {0}; int sum_r[maxn] = {0}; int L = 0; int counter_l = 0, counter_r = 0; stack<int> s; stack<char> star; int main() { int T = 0; cin >> T; while(T--) { cin >> S; L = strlen(S); counter_l = 0, counter_r = 0; while(s.size() > 0) { s.pop(); } for(int i = 0;i<L;i++) { if(S[i] == '(') { s.push(i); ima[i] = 0; } if(S[i] == ')') { if(s.size() > 0) { int pos = s.top(); s.pop(); ima[pos] = 1; ima[i] = 1; } else { ima[i] = 0; continue; } } if(S[i] == '*') { ima[i] = 0; continue; } } for(int i = 0;i<L;i++) { if(ima[i] || S[i] == '*') { continue; } else { if(S[i] == '(') counter_l++; if(S[i] == ')') counter_r++; } } int p = 0; int s1 = 0, s2 = 0; sum_r[L] = 0; sum_l[L] = 0; for(int i = 0;i<L;i++) { if(i == 0) { if(!ima[i] && S[i] == '(') { sum_l[0] = 1; } else { sum_l[0] = 0; } continue; } sum_l[i] = sum_l[i - 1]; if(!ima[i] && S[i] == '(') { sum_l[i] += 1; } } for(int i = L-1;i>=0;i--) { sum_r[i] = sum_r[i+1]; if(!ima[i] && S[i] == ')') { sum_r[i] += 1; } } bool flag = 1; while(star.size() > 0) { star.pop(); } for(p = 0;p<L;p++) { if(ima[p]) { continue; } if(S[p] == '(') { break; } if(S[p] == ')') { if(star.size() < counter_r - sum_r[p+1]) { flag = 0; break; } } if(S[p] == '*') { star.push('*'); } if(S[p] == '*' && s1 < counter_r) { S[p] = '('; s1 += 1; } } while(star.size() > 0) { star.pop(); } for(p = L-1;p>=0;p--) { if(ima[p]) { continue; } if(S[p] == ')') { break; } if(S[p] == '(') { if(star.size() < counter_l-sum_l[p-1]) { flag = 0; break; } } if(S[p] == '*') star.push('*'); if(S[p] == '*' && s2 < counter_l) { S[p] = ')'; s2 += 1; } } if(!(s1 == counter_r && s2 == counter_l && flag)) { cout << "No solution!" <<endl; } else { for(int i = 0;i<L;i++) { if(S[i] == '*') { continue; } cout << S[i]; } cout << endl; } } return 0; }