#include <iostream>
#include <stack>
#include <algorithm>
#include <string>
#include <vector>
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];
int check(int in) {
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;
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 ch = check(i);
if (ch == -1) {
cout << -1;
return;
}
w[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 |
Incorrect |
0 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 |
0 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 |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |