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<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 100005;
string str;
int dp[MAXN][26], strLen;
bool isOpen[MAXN];
bool solve(int l, int r) {
if(l > r)
return true;
int x(dp[r][str[l] - 'a']);
if(x <= l)
return false;
isOpen[l] = 1, isOpen[x] = 0;
return (solve(l + 1, x - 1) && solve(x + 1, r));
}
void process(void) {
cin >> str;
strLen = sz(str);
str = '^' + str;
for (int i = 0; i <= strLen; ++i) {
for (int j = 0; j < 26; ++j)
dp[i][j] = -1;
}
for (int i = 1; i <= strLen; ++i) {
int val(str[i] - 'a');
for (int j = 0; j < 26; ++j) {
dp[i][j] = (j == val) ? i : ((dp[i - 1][val] >= 1) ? dp[dp[i - 1][val] - 1][j] : -1);
}
}
bool check = solve(1, strLen);
if(!check) {
cout << "-1\n";
return;
}
for (int i = 1; i <= strLen; ++i)
cout << (isOpen[i] ? '(' : ')');
}
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
process();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |