#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) { return !ok(ind+1,(sz(v)?v.back():sz(x))-1); }
void ad(char c) { v.pb(pre[sz(v) ? v.back() : sz(x)][c-'a']); }
void rem() { v.pop_back(), v.pop_back(); }
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> x;
F0R(i,sz(x)+1) {
st[i][0] = (i?pre[i-1][x[i-1]-'a']:-1);
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;
}
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); }
string ans;
F0R(i,sz(x)) {
ad(x[i]);
if (bad(i)) rem(), ans += ')';
else ans += '(';
}
cout << ans;
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
676 KB |
Output is correct |
5 |
Correct |
4 ms |
676 KB |
Output is correct |
6 |
Correct |
3 ms |
748 KB |
Output is correct |
7 |
Correct |
3 ms |
824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
676 KB |
Output is correct |
5 |
Correct |
4 ms |
676 KB |
Output is correct |
6 |
Correct |
3 ms |
748 KB |
Output is correct |
7 |
Correct |
3 ms |
824 KB |
Output is correct |
8 |
Correct |
4 ms |
1592 KB |
Output is correct |
9 |
Correct |
4 ms |
1720 KB |
Output is correct |
10 |
Correct |
5 ms |
1736 KB |
Output is correct |
11 |
Correct |
5 ms |
1736 KB |
Output is correct |
12 |
Correct |
27 ms |
11096 KB |
Output is correct |
13 |
Correct |
25 ms |
11868 KB |
Output is correct |
14 |
Correct |
34 ms |
12764 KB |
Output is correct |
15 |
Correct |
28 ms |
14556 KB |
Output is correct |
16 |
Correct |
31 ms |
14572 KB |
Output is correct |
17 |
Correct |
29 ms |
15340 KB |
Output is correct |
18 |
Correct |
35 ms |
16272 KB |
Output is correct |
19 |
Correct |
36 ms |
17068 KB |
Output is correct |
20 |
Correct |
24 ms |
17068 KB |
Output is correct |
21 |
Correct |
38 ms |
17968 KB |
Output is correct |