-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloai_bo_dau_ngoac.cpp
More file actions
75 lines (75 loc) · 1.53 KB
/
loai_bo_dau_ngoac.cpp
File metadata and controls
75 lines (75 loc) · 1.53 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
bool Check(string str)
{
if (str.size() == 0)
return false;
if (str.size() == 1 && isalpha(str[0]))
return false;
int cnt = 0;
for (int i = 0; i < (int)str.length(); i++)
{
if (str[i] == '(')
cnt++;
else if (str[i] == ')')
cnt--;
if (cnt < 0)
return false;
}
return (cnt == 0);
}
void Solve()
{
string s;
cin >> s;
vector<string> ans;
unordered_set<string> diff;
queue<string> q;
diff.insert(s);
q.push(s);
bool mn = false;
while (!q.empty())
{
string str = q.front();
q.pop();
if (Check(str))
{
ans.push_back(str);
mn = true;
}
if (mn)
continue;
for (int i = 0; i < (int)str.size(); i++)
{
if (isalnum(str[i]))
continue;
string tmp = str.substr(0, i) + str.substr(i + 1);
if (diff.find(tmp) == diff.end())
{
diff.insert(tmp);
q.push(tmp);
}
}
}
if (ans.size() == 0)
{
cout << -1 << "\n";
return;
}
sort(ans.begin(), ans.end());
for (auto x : ans)
cout << x << " ";
cout << "\n";
}
int main()
{
int t;
cin >> t;
while (t--)
{
Solve();
}
return 0;
}