forked from yalza/DSA-code.ptit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSA02015 - LOẠI BỎ DẤU NGOẶC.cpp
62 lines (60 loc) · 1.08 KB
/
DSA02015 - LOẠI BỎ DẤU NGOẶC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<unordered_set>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> res;
bool ktra(string s) {
int x = 0, y = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(') {
x++;
y++;
}
if (s[i] == ')')x--;
if (x < 0)return false;
}
if (y == 0&&s.length()==1)return false;
return x == 0;
}
void BFS(string s) {
if (!s.length())return;
unordered_set<string> A;
queue<string> B;
A.insert(s);
B.push(s);
bool ok = false;
while (B.size()) {
string s = B.front();
B.pop();
if (ktra(s)&&s!="") {
res.push_back(s);
ok = true;
}
if (ok)continue;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == ')') {
string a = s.substr(0, i) + s.substr(i + 1, s.length());
if (A.find(a) == A.end()) {
A.insert(a);
B.push(a);
}
}
}
}
}
int main() {
int t; cin >> t;
while (t--) {
res.clear();
string s; cin >> s;
BFS(s);
if (res.size() == 0)cout << -1;
else {
sort(res.begin(), res.end());
for (auto s : res)cout << s << " ";
}
cout << endl;
}
}