답안 #129419

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129419 2019-07-12T08:17:44 Z PeppaPig 괄호 문자열 (CEOI16_match) C++14
100 / 100
20 ms 9732 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+5;

int n, col[N];
char S[N], ans[N];
stack<int> st;
vector<int> v[N];

void solve(int l, int r) {
    if(l > r) return;
    vector<int> &now = v[col[l]];
    auto it = --upper_bound(now.begin(), now.end(), r);
    ans[l] = '(', ans[*it] = ')';
    solve(l + 1, *it - 1), solve(*it + 1, r);
}

int t[N][26], par[N];

int main() {
    scanf("%s", S + 1);
    n = strlen(S + 1);
    int ptr = 0, u = 0;
    for(int i = 1; i <= n; i++) {
        int c = S[i] - 'a', id;
        if(st.empty() || st.top() != c) {
            st.emplace(c);
            if(!t[u][c]) t[u][c] = ++ptr, par[ptr] = u;
            id = t[u][c], u = t[u][c];
        } else {
            id = u, u = par[u];
            st.pop();
        }
        col[i] = id;
        v[id].emplace_back(i);
    }
    if(st.size()) return !printf("-1\n");
    solve(1, n);
    printf("%s\n", ans + 1);

    return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", S + 1);
     ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 4 ms 2684 KB Output is correct
6 Correct 4 ms 2808 KB Output is correct
7 Correct 4 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 4 ms 2684 KB Output is correct
6 Correct 4 ms 2808 KB Output is correct
7 Correct 4 ms 2808 KB Output is correct
8 Correct 5 ms 2936 KB Output is correct
9 Correct 6 ms 3196 KB Output is correct
10 Correct 6 ms 3192 KB Output is correct
11 Correct 5 ms 3192 KB Output is correct
12 Correct 16 ms 7544 KB Output is correct
13 Correct 14 ms 8060 KB Output is correct
14 Correct 16 ms 8440 KB Output is correct
15 Correct 10 ms 4856 KB Output is correct
16 Correct 10 ms 4856 KB Output is correct
17 Correct 14 ms 7420 KB Output is correct
18 Correct 10 ms 4348 KB Output is correct
19 Correct 18 ms 9720 KB Output is correct
20 Correct 13 ms 7676 KB Output is correct
21 Correct 20 ms 9732 KB Output is correct