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;
typedef long long ll;
const int N = 1e5 + 1;
const int AL = 26;
int n, match[N], pre[N][AL];
char answer[N];
string s;
void readInput(){
cin >> s;
n = s.size();
s = '*' + s;
}
void process(int l, int r){
if (l > r)
return;
int p = pre[r][s[l] - 'a'];
if (!p){
cout << -1;
exit(0);
}
answer[l] = '(';
answer[p] = ')';
process(l + 1, p - 1);
process(p + 1, r);
}
void solve(){
for(int i = 1; i <= n; i++)
for(int j = 0; j < AL; j++)
if (s[i] - 'a' == j)
pre[i][j] = i;
else
pre[i][j] = pre[max(0, pre[i - 1][s[i] - 'a'] - 1)][j];
process(1, n);
for(int i = 1; i <= n; i++)
cout << answer[i];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
readInput();
solve();
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... |