답안 #528790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
528790 2022-02-21T13:16:08 Z KoD 괄호 문자열 (CEOI16_match) C++17
100 / 100
16 ms 18688 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

template <class F> struct RecLambda : private F {
    explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args> decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::string S;
    std::cin >> S;
    const int N = S.size();
    vector<int> A(N);
    for (int i = 0; i < N; ++i) A[i] = S[i] - 'a';
    vector<array<int, 26>> memo(N + 1);
    vector<array<bool, 26>> done(N + 1);
    const auto rightmost = RecLambda([&](auto&& dfs, const int k, const int c) -> int {
        if (k == 0) return -1;
        if (done[k][c]) return memo[k][c]; 
        done[k][c] = true;
        const int b = A[k - 1];
        if (b == c) return memo[k][c] = k - 1;
        const int m = dfs(k - 1, b);
        if (m == -1) return memo[k][c] = -1;
        return memo[k][c] = dfs(m, c);
    });
    std::string ans;
    if (RecLambda([&](auto&& dfs, const int l, const int r) -> bool {
        if (l == r) return true;
        const int m = rightmost(r, A[l]);
        if (m == -1) return false;
        ans += '(';
        if (!dfs(l + 1, m)) return false;
        ans += ')';
        if (!dfs(m + 1, r)) return false;
        return true;
    })(0, N)) {
        std::cout << ans << '\n';
    } else {
        std::cout << "-1\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 592 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 592 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 2 ms 1232 KB Output is correct
9 Correct 2 ms 1488 KB Output is correct
10 Correct 2 ms 1488 KB Output is correct
11 Correct 1 ms 1744 KB Output is correct
12 Correct 8 ms 11312 KB Output is correct
13 Correct 8 ms 12496 KB Output is correct
14 Correct 10 ms 14024 KB Output is correct
15 Correct 10 ms 17296 KB Output is correct
16 Correct 10 ms 17268 KB Output is correct
17 Correct 11 ms 17940 KB Output is correct
18 Correct 10 ms 15000 KB Output is correct
19 Correct 16 ms 16888 KB Output is correct
20 Correct 9 ms 12096 KB Output is correct
21 Correct 16 ms 18688 KB Output is correct