Submission #446016

#TimeUsernameProblemLanguageResultExecution timeMemory
446016sinamhdvMatch (CEOI16_match)C++11
100 / 100
9 ms12636 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...