I - Parentheses Matching

https://vjudge.net/contest/386223#problem/I

Given a string  PP 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 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:

    ● 1kn1≤k≤n, and
    ● the first (k1)(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 (1T1051≤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 (1n1051≤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;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值