Submission #244093

# Submission time Handle Problem Language Result Execution time Memory
244093 2020-07-02T15:04:42 Z tonowak Match (CEOI16_match) C++17
10 / 100
5 ms 416 KB
#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<void (int, int)> divconq = [&](int l, int r) {
		if(l >= r)
			return;
		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);
				answer[l] = 0;
				answer[*it] = 1;
				divconq(l + 1, *it - 1);
				divconq(*it + 1, r);
				return;
			}
		cout << "-1\n";
		exit(0);
	};
	divconq(0, n - 1);

	for(bool b : answer)
		cout << (b ? ')' : '(');
	cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 416 KB Output is correct
4 Incorrect 5 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 416 KB Output is correct
4 Incorrect 5 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -