Parentheses Matching HDU - 6799 deque+stack

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

(())()(())

思路:用stack存其中的一个括号,然后deque存*,进行匹配

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 50;
const int N = 110;
const int INF = 0x3f3f3f3f;
const int inf = 0xfffffff;//比INF小,防止累加爆int
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
const int mod = 193;
int gcd(int x, int y) {
    
    
	return y ? gcd(y, x % y) : x;
}
char s[maxn];
stack<int>stk;
deque<int>d;
void init() {
    
    
	while (!stk.empty()) {
    
    
		stk.pop();
	}
	while (!d.empty()) {
    
    
		d.pop_back();
	}
}
int main() {
    
    
	speed;
	int t;
	cin >> t;
	while (t--) {
    
    
		init();
		cin >> s + 1;
		int len = strlen(s + 1);
		int flag = 1;
		for (int i = 1; i <= len; ++i) {
    
    
			if (s[i] == '*')d.push_back(i);
			if (s[i] == '(')stk.push(i);
			if (s[i] == ')') {
    
    
				if (!stk.empty())stk.pop();
				else if (!d.empty()) {
    
    
					s[d.front()] = '(';
					d.pop_front();
				}
				else {
    
    
					flag = 0;
					break;
				}
			}
		}
		while (!stk.empty()) {
    
    
			if (!d.empty() && d.back() > stk.top()) {
    
    
				s[d.back()] = ')';
				d.pop_back();
				stk.pop();
			}
			else {
    
    
				flag = 0;
				break;
			}
		}
		if (!flag)cout << "No solution!" << endl;
		else {
    
    
			for (int i = 1; i <= len; ++i) {
    
    
				if (s[i] == '(' || s[i] == ')')
					cout << s[i];
			}
			cout << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40924271/article/details/107941818