답안 #859897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
859897 2023-10-11T04:13:18 Z Nhoksocqt1 괄호 문자열 (CEOI16_match) C++17
100 / 100
9 ms 12632 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 100005;

string str;
int dp[MAXN][26], strLen;
bool isOpen[MAXN];

bool solve(int l, int r) {
    if(l > r)
        return true;

    int x(dp[r][str[l] - 'a']);
    if(x <= l)
        return false;

    isOpen[l] = 1, isOpen[x] = 0;
    return (solve(l + 1, x - 1) && solve(x + 1, r));
}

void process(void) {
    cin >> str;
    strLen = sz(str);
    str = '^' + str;

    for (int i = 0; i <= strLen; ++i) {
        for (int j = 0; j < 26; ++j)
            dp[i][j] = -1;
    }

    for (int i = 1; i <= strLen; ++i) {
        int val(str[i] - 'a');
        for (int j = 0; j < 26; ++j) {
            dp[i][j] = (j == val) ? i : ((dp[i - 1][val] >= 1) ? dp[dp[i - 1][val] - 1][j] : -1);
        }
    }

    bool check = solve(1, strLen);
    if(!check) {
        cout << "-1\n";
        return;
    }

    for (int i = 1; i <= strLen; ++i)
        cout << (isOpen[i] ? '(' : ')');
}

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

    process();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 5 ms 7772 KB Output is correct
13 Correct 6 ms 10116 KB Output is correct
14 Correct 6 ms 10328 KB Output is correct
15 Correct 7 ms 11096 KB Output is correct
16 Correct 7 ms 11096 KB Output is correct
17 Correct 8 ms 11544 KB Output is correct
18 Correct 9 ms 10720 KB Output is correct
19 Correct 9 ms 11608 KB Output is correct
20 Correct 5 ms 8028 KB Output is correct
21 Correct 9 ms 12632 KB Output is correct