답안 #741002

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
741002 2023-05-13T11:28:36 Z 이동현(#9959) 괄호 문자열 (CEOI16_match) C++17
100 / 100
514 ms 235812 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

const int NS = (int)1e5 + 4;
string s;
int n;
int spa[NS][30][20];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> s;
    n = (int)s.size();
    memset(spa, -1, sizeof(spa));
    for(int i = n - 3; i >= 0; --i){
        int v = -1;
        if(s[i] == s[i + 1]){
            v = spa[i][s[i + 2] - 'a'][0] = i + 1;
        }
        else{
            int p = spa[i + 1][s[i] - 'a'][0];
            if(p != -1 && p + 2 < n){
                v = spa[i][s[p + 2] - 'a'][0] = p + 1;
            }
        }
        for(int j = 0; j < 26; ++j){
            if(v != -1 && spa[i][j][0] == -1){
                spa[i][j][0] = spa[v + 1][j][0];
            }
        }
    }
    for(int j = 0; j < 26; ++j){
        for(int k = 1; k < 20; ++k){
            for(int i = 0; i < n; ++i){
                spa[i][j][k] = (spa[i][j][k - 1] == -1 ? -1 : spa[spa[i][j][k - 1] + 1][j][k - 1]);
            }
        }
    }

    string ans(n, '$');
    priority_queue<int, vector<int>, greater<int>> pq;
    for(int i = 0; i < n; ++i){
        if(!pq.empty() && pq.top() <= i){
            pq.pop();
            continue;
        }

        int lim = n + 243;
        if(!pq.empty()) lim = pq.top();
        int now = i + 1;
        for(int k = 19; k >= 0; --k){
            if(spa[now][s[i] - 'a'][k] != -1 && spa[now][s[i] - 'a'][k] + 1 < lim){
                now = spa[now][s[i] - 'a'][k] + 1;
            }
        }

        if(now >= lim || s[i] != s[now]){
            cout << "-1\n";
            return 0;
        }

        ans[i] = '(';
        ans[now] = ')';
        pq.push(now);
    }

    cout << ans << '\n';
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 235036 KB Output is correct
2 Correct 98 ms 235048 KB Output is correct
3 Correct 93 ms 235064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 235036 KB Output is correct
2 Correct 98 ms 235048 KB Output is correct
3 Correct 93 ms 235064 KB Output is correct
4 Correct 87 ms 235100 KB Output is correct
5 Correct 92 ms 235064 KB Output is correct
6 Correct 92 ms 235092 KB Output is correct
7 Correct 88 ms 235096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 235036 KB Output is correct
2 Correct 98 ms 235048 KB Output is correct
3 Correct 93 ms 235064 KB Output is correct
4 Correct 87 ms 235100 KB Output is correct
5 Correct 92 ms 235064 KB Output is correct
6 Correct 92 ms 235092 KB Output is correct
7 Correct 88 ms 235096 KB Output is correct
8 Correct 109 ms 235132 KB Output is correct
9 Correct 102 ms 235084 KB Output is correct
10 Correct 102 ms 235056 KB Output is correct
11 Correct 116 ms 235084 KB Output is correct
12 Correct 260 ms 235512 KB Output is correct
13 Correct 282 ms 235468 KB Output is correct
14 Correct 363 ms 235780 KB Output is correct
15 Correct 514 ms 235688 KB Output is correct
16 Correct 434 ms 235692 KB Output is correct
17 Correct 467 ms 235740 KB Output is correct
18 Correct 462 ms 235596 KB Output is correct
19 Correct 461 ms 235712 KB Output is correct
20 Correct 261 ms 235632 KB Output is correct
21 Correct 509 ms 235812 KB Output is correct