#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
string x;
int st[MX][17], pre[MX][26];
bool ok(int l, int r) {
r ++;
F0Rd(i,17) if (st[r][i] >= l) r = st[r][i];
return l == r;
}
vi v;
bool bad(int ind) {
if(v.size()&&ind==v.back()) return 1;
else return 0;
}
void ad(char c) {
v.pb(pre[sz(v) ? v.back() : sz(x)][c-'a']);
}
void rem() {
v.pop_back();
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> x;
F0R(i,sz(x)+1) {
if (i) st[i][0] = pre[i-1][x[i-1]-'a'];
else st[i][0] = -1;
// if (i) cout << i << " " << st[i][0] << " " << x[i-1] << "\n";
F0R(j,26) pre[i][j] = (st[i][0] == -1 ? -1 : pre[st[i][0]][j]);
if (i) {
pre[i][x[i-1]-'a'] = i-1;
// cout << "AH " << i << " " << x[i-1] << "\n";
}
}
FOR(j,1,17) F0R(i,sz(x)+1) {
if (st[i][j-1] == -1) st[i][j] = -1;
else st[i][j] = st[st[i][j-1]][j-1];
}
if (!ok(0,sz(x)-1)) {
cout << -1;
exit(0);
}
// cout << pre[6][0]; exit(0);
string ans;
F0R(i,sz(x)) {
//ad(x[i]);
//cout << v.back() << " " << bad(i) << "\n";
//exit(0);
if (bad(i)) {
rem();
ans += ')';
} else {
ad(x[i]);
ans += '(';
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
3 ms |
748 KB |
Output is correct |
5 |
Correct |
5 ms |
816 KB |
Output is correct |
6 |
Correct |
4 ms |
816 KB |
Output is correct |
7 |
Correct |
4 ms |
832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
488 KB |
Output is correct |
4 |
Correct |
3 ms |
748 KB |
Output is correct |
5 |
Correct |
5 ms |
816 KB |
Output is correct |
6 |
Correct |
4 ms |
816 KB |
Output is correct |
7 |
Correct |
4 ms |
832 KB |
Output is correct |
8 |
Correct |
5 ms |
1728 KB |
Output is correct |
9 |
Correct |
7 ms |
1736 KB |
Output is correct |
10 |
Correct |
6 ms |
1888 KB |
Output is correct |
11 |
Correct |
6 ms |
1888 KB |
Output is correct |
12 |
Correct |
30 ms |
11196 KB |
Output is correct |
13 |
Correct |
28 ms |
12320 KB |
Output is correct |
14 |
Correct |
31 ms |
13124 KB |
Output is correct |
15 |
Correct |
36 ms |
14880 KB |
Output is correct |
16 |
Correct |
26 ms |
14880 KB |
Output is correct |
17 |
Correct |
31 ms |
15648 KB |
Output is correct |
18 |
Correct |
39 ms |
16416 KB |
Output is correct |
19 |
Correct |
36 ms |
17312 KB |
Output is correct |
20 |
Correct |
24 ms |
17312 KB |
Output is correct |
21 |
Correct |
62 ms |
18208 KB |
Output is correct |