#include <iostream>
#include <stack>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int n;
string s;
stack <char> S;
string generuj(string w, int in) {
int sum = 0;
for (int i = 0; i < in; i++) {
if (w[i] == '(') {
sum++;
S.push(s[i]);
}
else {
sum--;
S.pop();
}
}
w[in] = '(';
S.push(s[in]);
sum++;
for (int i = in + 1; i < n; i++) {
if (S.empty()) {
sum++;
w.insert(i, "(");
S.push(s[i]);
}
else {
char v = S.top();
if (v == s[i]) {
sum--;
S.pop();
w[i] = ')';
}
else {
sum++;
w[i] = '(';
S.push(s[i]);
}
}
}
while (!S.empty()) S.pop();
if (sum != 0) {
return "))))))))))))))))))";
}
else return w;
}
void task() {
int sum = 0;
string w = "";
for (int i = 0; i < n; i++) {
if (S.empty()) {
sum++;
w.insert(w.size(), "(");
S.push(s[i]);
}
else {
char v = S.top();
if (v == s[i]) {
sum--;
S.pop();
w.insert(w.size(), ")");
}
else {
sum++;
w.insert(w.size(), "(");
S.push(s[i]);
}
}
}
if (sum != 0) {
cout << -1;
return;
}
for (int i = 1; i < n; i++) {
if (w[i] == ')') {
w = min( w, generuj(w, i));
}
}
cout << w;
return;
}
int main()
{
cin >> s;
n = s.size();
task();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |