#include <iostream>
#include <stack>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
int const N = 1e5 + 9;
int n;
string s, w;
stack <char> S;
int ile[N][30];
int nt[30];
vector<int> V[30];
priority_queue<int> Q;
int check(int in, int end) {
int a = s[in] - 'a';
for (int i = V[a].size() - 1; i >= 0; i--) {
int nIn = V[a][i];
if (nIn <= in) return -1;
if (w[nIn] == ')') continue;
if (nIn > end) continue;
bool t = true;
for (int j = 0; j < 30; j++) {
nt[j] = ile[nIn][j];
if(in > 0)
nt[j] -= ile[in - 1][j];
if (nt[j] % 2 == 1) t = false;
}
if (t == true) return nIn;
}
return -1;
}
void task() {
w = "";
for (int i = 0; i < n; i++) {
V[s[i] - 'a'].push_back(i);
if (i > 0) {
for (int j = 0; j < 30; j++)
ile[i][j] = ile[i - 1][j];
}
ile[i][s[i] - 'a']++;
w.insert(w.size(), "0");
}
for (int i = 0; i < n; i++) {
if (w[i] == ')') continue;
w[i] = '(';
int e;
if (Q.empty()) e = n;
else {
e = -Q.top();
while (e < i) {
Q.pop();
if (Q.empty()) {
e = n;
break;
}
e = -Q.top();
}
}
int ch = check(i, e);
if (ch == -1) {
cout << -1;
return;
}
w[ch] = ')';
Q.push(-ch);
}
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 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |