답안 #980336

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
980336 2024-05-12T05:16:39 Z LOLOLO 괄호 문자열 (CEOI16_match) C++17
100 / 100
37 ms 19296 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 1e5 + 10;
int n;

bool check(string s) {
    stack <char> st;
    for (int i = 1; i <= n; i++) {
        if (st.empty() || (st.top() != s[i])) {
            st.push(s[i]);
        } else {
            st.pop();
        }
    }

    return st.empty();
}

vector <ll> coor;
vector <int> pos[N];
ll p[N], mod = 1e11 + 7, base = 37;
char tt[N];
int sp[N][30];
string s;

void dnc(int l, int r) {
    if (l >= r)
        return;

    int m = (sp[r][s[l] - 'a']);
    if (s[l] == s[r]) {
        m = r;
    }

    tt[l] = '(';
    tt[m] = ')';
    dnc(l + 1, m - 1);
    dnc(m + 1, r);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    p[0] = 1;
    for (int i = 1; i < N; i++) {
        p[i] = (p[i - 1] * base) % mod;
    }

    cin >> s;

    s = '0' + s;
    n = len(s) - 1;

    if (check(s) == 0) {
        cout << -1 << '\n';
        return 0;
    }

    stack <char> st;
    ll all = 0;

    coor.pb(all);
    for (int i = 1; i <= n; i++) {
        if (st.empty() || (st.top() != s[i])) {
            st.push(s[i]);
            all = (all + p[sz(st)] * (ll)(st.top() - 'a' + 1));
        } else {
            all = (all - p[sz(st)] * (ll)(st.top() - 'a' + 1));
            st.pop();
        }

        while (all < 0)
            all += mod;

        while (all >= mod)
            all -= mod;

        coor.pb(all);
    }

    uniquev(coor);
    all = 0;
    while (sz(st))
        st.pop();

    for (int i = 1; i <= n; i++) {
        if (st.empty() || (st.top() != s[i])) {
            st.push(s[i]);
            all = (all + p[sz(st)] * (ll)(st.top() - 'a' + 1));
        } else {
            all = (all - p[sz(st)] * (ll)(st.top() - 'a' + 1));
            st.pop();
        }

        while (all < 0)
            all += mod;

        while (all >= mod)
            all -= mod;

        int t = lower_bound(all(coor), all) - coor.begin();
        pos[t].pb(i);
    }

    for (int i = 0; i <= n; i++) {
        vector <int> st(30, 0);
        for (auto x : pos[i]) {
            for (int j = 0; j < 26; j++) {
                sp[x][j] = st[j];
            }
 
            st[s[x] - 'a'] = x;
        }
    }

    dnc(1, n);

    for (int i = 1; i <= n; i++)
        cout << tt[i];

    return 0;
}  
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4952 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4952 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 7260 KB Output is correct
7 Correct 3 ms 7260 KB Output is correct
8 Correct 4 ms 7260 KB Output is correct
9 Correct 4 ms 7260 KB Output is correct
10 Correct 4 ms 7260 KB Output is correct
11 Correct 4 ms 7572 KB Output is correct
12 Correct 21 ms 15820 KB Output is correct
13 Correct 23 ms 16084 KB Output is correct
14 Correct 25 ms 16400 KB Output is correct
15 Correct 16 ms 18704 KB Output is correct
16 Correct 16 ms 18704 KB Output is correct
17 Correct 28 ms 19188 KB Output is correct
18 Correct 19 ms 17672 KB Output is correct
19 Correct 35 ms 18988 KB Output is correct
20 Correct 24 ms 16040 KB Output is correct
21 Correct 37 ms 19296 KB Output is correct