답안 #116856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
116856 2019-06-14T02:51:40 Z minhtung0404 괄호 문자열 (CEOI16_match) C++17
100 / 100
20 ms 12288 KB
//https://oj.uz/problem/view/CEOI16_match

#include<bits/stdc++.h>
const int N = 1e5 + 5;
using namespace std;

string s, x;
int n, nxt[N][26], f[N];

void solve(int l, int r){
    if (l > r) return;
    int mat = nxt[r][s[l] - 'a'];
    x[l] = '('; x[mat] = ')';
    solve(l+1, mat-1); solve(mat+1, r);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> s;
    n = s.size();
    for (int i = 0; i < n; i++){
        if (i == 0) f[i] = -1;
        else f[i] = nxt[i-1][s[i] - 'a'];
        for (int j = 0; j < 26; j++){
            if (s[i] - 'a' == j) nxt[i][j] = i;
            else{
                if (f[i] > 0) nxt[i][j] = nxt[f[i]-1][j];
                else nxt[i][j] = -1;
            }
        }
    }
    stack <int> ms;
    for (int i = 0; i < n; i++){
        if (ms.size() && ms.top() == s[i] - 'a') ms.pop();
        else ms.push(s[i] - 'a');
    }
    if (ms.size()) return cout << -1, 0;
    x = s;
    solve(0, n-1);
    cout << x;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 1024 KB Output is correct
9 Correct 3 ms 1152 KB Output is correct
10 Correct 3 ms 1152 KB Output is correct
11 Correct 3 ms 1280 KB Output is correct
12 Correct 11 ms 7440 KB Output is correct
13 Correct 12 ms 8064 KB Output is correct
14 Correct 15 ms 8832 KB Output is correct
15 Correct 15 ms 10104 KB Output is correct
16 Correct 14 ms 10112 KB Output is correct
17 Correct 16 ms 10740 KB Output is correct
18 Correct 17 ms 10616 KB Output is correct
19 Correct 19 ms 11520 KB Output is correct
20 Correct 12 ms 7552 KB Output is correct
21 Correct 20 ms 12288 KB Output is correct