HDU 6799 Parentheses Matching (2020杭电多校训练第三场)

HDU 6799 Parentheses Matching (2020杭电多校训练第三场)

杭电OJ6799

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! () ()()

(())()(())

简单的数据结构基础,仅用到队列的先进先出和栈的先进后出的原则
就是括号的匹配的问题,我们只需要从左到右进行匹配,再从右到左进行匹配即可。
用队列q来储存前面读到的“*” ,栈k储存括号(从左到右储存“(”,从右到左储存“)”)。
每次循环都要把队列和栈清空(由于这个wa了好多发)。
废话不多说,直接上ac代码:

#include 
#define rep(i,a,b) for(int i=a;i
#define T int t ;cin >> t;while(t--)

using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-11;
const ll mod = 1e9 + 7;
queue<int> q ;
stack<int> k ;
int main()
{
	int t ;
	cin >> t ;
	while(t--)
	{
		int flag = 1 ;
		char a[maxn] ;
		scanf("%s",a) ;
		while(!q.empty())
		q.pop();
		while(!k.empty())
		k.pop();
		ll xlen = strlen(a) ;
		ll high = 0 , low = 0 ;
		for(int i = 0 ; i < xlen ; i++)
		{
			if(a[i] == '(')
				k.push(i) ;
			if(a[i] == '*')
				q.push(i) ;	
			if(a[i] == ')')
			{
				if(k.size())
				{
					k.pop();
				}
				else 
				{
					if(!q.empty())
					{
						int xx=q.front();
						q.pop();
						a[xx]='(';
					}
					else
						flag = 0 ;
				}
			}
		}	
		if(!flag)
        {
            printf("No solution!\n");
            continue;
        }
		while(!q.empty())
		q.pop();
		while(!k.empty())
		k.pop();
		for(int i = xlen-1 ; i >= 0 ; i--)
		{
			if(a[i] == ')')
				k.push(i) ;
			else if(a[i] == '*')
				q.push(i) ;
			else if(a[i] == '(')
			{
				if(k.size())
				{
					k.pop() ;
				}
				else
				{
					if(!q.empty())
					{
						int xx=q.front();
						q.pop();
						a[xx]=')';
					}
					else 
						flag = 0 ;
				}	
			}	
		}
		if(!flag&&k.empty())
			printf("No solution!\n") ;
		else
		{
			for(int i = 0 ; i < xlen ; i++)
			{
				if(a[i]!='*')
					printf("%c",a[i]) ;
			}
			printf("\n") ;
		}	
	}
	return 0 ;
}

你可能感兴趣的:(题解,数据结构)