#include <bits/stdc++.h> // Tomasz Nowak
using namespace std; // XIII LO Szczecin
using LL = long long; // Poland
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
out << '{';
for(auto it = x.begin(); it != x.end(); ++it)
out << *it << (it == prev(x.end()) ? "" : ", ");
return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
#ifdef DEBUG
struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
// end of templates
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n = size(s);
vector<int> value(n);
REP(i, n)
value[i] = int(s[i] - 'a');
constexpr int alpha = int('z' - 'a' + 1);
using BS = bitset<alpha>;
BS empty;
vector<BS> prefix(n);
vector<vector<int>> at(alpha);
REP(i, n)
at[value[i]].emplace_back(i);
REP(i, n) {
if(i > 0)
prefix[i] = prefix[i - 1];
prefix[i].flip(value[i]);
}
auto get = [&](int l, int r) {
if(l == 0)
return prefix[r] == empty;
return (prefix[r] ^ prefix[l - 1]) == empty;
};
vector<bool> answer(n);
function<bool (int, int)> divconq = [&](int l, int r) {
if(l > r)
return true;
debug(l, r);
auto it = upper_bound(at[value[l]].begin(), at[value[l]].end(), r);
assert(it != at[value[l]].begin());
--it;
for(; *it > l; --it)
if(get(l, *it)) {
debug(l, *it);
if(not divconq(l + 1, *it - 1))
continue;
if(not divconq(*it + 1, r))
continue;
answer[l] = 0;
answer[*it] = 1;
return true;
}
return false;
};
if(not divconq(0, n - 1)) {
cout << "-1\n";
return 0;
}
for(bool b : answer)
cout << (b ? ')' : '(');
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Execution timed out |
2082 ms |
384 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Execution timed out |
2082 ms |
384 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |