Submission #446016

# Submission time Handle Problem Language Result Execution time Memory
446016 2021-07-20T13:35:50 Z sinamhdv Match (CEOI16_match) C++11
100 / 100
9 ms 12636 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fr first
#define sc second
#define endl '\n'

const int MAXN = 100100;
const int ALP = 26;

int n;
string s;
string ans;
int rmat[MAXN][ALP];

void solve(int l, int r)
{
	if (l > r) return;
	int p = rmat[r][s[l] - 'a'];
	ans[l] = '(';
	ans[p] = ')';
	solve(l + 1, p - 1);
	solve(p + 1, r);
}

int32_t main(void)
{
	fast_io;
	cin >> s;
	n = s.size();

	vector<int> stk;
	FOR(i, 0, n)
	{
		if (!stk.empty() && stk.back() == s[i]) stk.pop_back();
		else stk.pb(s[i]);
	}
	if (!stk.empty()) return cout << "-1\n", 0;

	ans.resize(n);

	// prep rmat
	rmat[0][s[0] - 'a'] = 0;
	FOR(i, 1, n)
	{
		int p = rmat[i - 1][s[i] - 'a'];
		if (p <= 0) memset(rmat[i], -1, sizeof(rmat[i]));
		else memcpy(rmat[i], rmat[p - 1], sizeof(rmat[i]));
		rmat[i][s[i] - 'a'] = i;
	}

	solve(0, n - 1);

	cout << ans << endl;

	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 972 KB Output is correct
9 Correct 1 ms 1100 KB Output is correct
10 Correct 1 ms 1100 KB Output is correct
11 Correct 1 ms 1228 KB Output is correct
12 Correct 6 ms 7756 KB Output is correct
13 Correct 5 ms 8396 KB Output is correct
14 Correct 7 ms 9292 KB Output is correct
15 Correct 8 ms 10692 KB Output is correct
16 Correct 8 ms 10704 KB Output is correct
17 Correct 8 ms 11348 KB Output is correct
18 Correct 8 ms 10572 KB Output is correct
19 Correct 8 ms 11592 KB Output is correct
20 Correct 5 ms 8012 KB Output is correct
21 Correct 9 ms 12636 KB Output is correct