Submission #61870

# Submission time Handle Problem Language Result Execution time Memory
61870 2018-07-27T02:21:05 Z Benq Match (CEOI16_match) C++11
100 / 100
49 ms 18860 KB
#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) {
        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 {
            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 time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 4 ms 568 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 4 ms 748 KB Output is correct
7 Correct 3 ms 776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 4 ms 568 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 4 ms 748 KB Output is correct
7 Correct 3 ms 776 KB Output is correct
8 Correct 7 ms 1672 KB Output is correct
9 Correct 5 ms 1876 KB Output is correct
10 Correct 5 ms 1876 KB Output is correct
11 Correct 5 ms 1876 KB Output is correct
12 Correct 24 ms 11040 KB Output is correct
13 Correct 26 ms 12060 KB Output is correct
14 Correct 29 ms 12936 KB Output is correct
15 Correct 34 ms 14772 KB Output is correct
16 Correct 32 ms 14884 KB Output is correct
17 Correct 34 ms 15860 KB Output is correct
18 Correct 46 ms 16840 KB Output is correct
19 Correct 46 ms 17668 KB Output is correct
20 Correct 27 ms 17668 KB Output is correct
21 Correct 49 ms 18860 KB Output is correct