제출 #61871

#제출 시각아이디문제언어결과실행 시간메모리
61871Benq괄호 문자열 (CEOI16_match)C++11
100 / 100
38 ms17968 KiB

#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
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...