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 <iostream>
#include <stack>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int const N = 1e5 + 9;
int n;
string s;
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;
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() {
string 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |