I Parentheses Matching(2020杭电多校训练第三场)
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 792 Accepted Submission(s): 345
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=AB, 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≤k≤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≤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 n (1≤n≤105), 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×106.
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!
()
()()
(())()(())
题意:* 可以变成( 或者 ),问能否有能变成最小字典序的左右括号都配对的情况。
题解
字典序最小那么(应该在最前面,)应该在最后面,用队列存*用栈存(,然后进行匹配配对即可,最后再从右到左把没配对的)补上
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#define ll long long
using namespace std;
const int INF=0x3f3f3f3f;
const double pi=acos(-1.0),eps=1e-8;
const int maxn=1e5+10;
const ll mod=1e9+7;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int f=1;
string x;
cin>>x;
queue<int>q;
stack<int>stk;
for(int i=0; i<x.size(); i++)
{
if(x[i]=='*')
q.push(i);
if(x[i]=='(')
stk.push(i);
if(x[i]==')')
{
if(stk.size())
{
stk.pop();
}
else
{
if(!q.empty())
{
int xx=q.front();
q.pop();
x[xx]='(';
}
else
{
// printf("!!");
f=0;
break;
}
}
}
}
if(!f)
{
printf("No solution!\n");
continue;
}
if(stk.size())
{
int cnt=0,num[100005];
while(!q.empty())
{
num[cnt++]=q.front();
q.pop();
}
if(cnt>=stk.size())
{
cnt--;
while(stk.size())
{
int xx=stk.top();
stk.pop();
if(num[cnt]<xx)
{
//printf("%d %d\n",num[cnt],xx);
f=0;
break;
}
x[num[cnt]]=')';
cnt--;
}
}
else
{
//printf("!!");
f=0;
}
}
if(f)
{
for(int i=0; i<x.size(); i++)
{
if(x[i]!='*')
printf("%c",x[i]);
}
printf("\n");
}
else
{
printf("No solution!\n");
}
}
return 0;
}