This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define ld long double
using namespace std;
int n; string s;
bool possible(stack<char> open, int idx) {
for (int i = idx; i < n; i++) {
if (!open.empty() && open.top() == s[i])
open.pop();
else
open.push(s[i]);
}
return open.empty();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("match.in", "r", stdin);
//freopen("match.out", "w", stdout);
cin >> s;
n = s.size();
if (!possible(stack<char>(), 0)) {
cout << "-1\n";
return 0;
}
vector<vector<int> > where(26);
for (int i = 0; i < n; i++) {
where[s[i] - 'a'].push_back(i);
}
string out = "";
stack<char> open;
for (int i = 0; i < n; i++) {
if (!open.empty() && s[i] == open.top()) {
open.push(s[i]);
if (possible(open, i + 1))
out += '(';
else
out += ')', open.pop(), open.pop();
}
else
open.push(s[i]), out += '(';
}
cout << out << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |