Submission #134128

# Submission time Handle Problem Language Result Execution time Memory
134128 2019-07-22T06:18:45 Z imeimi2000 Match (CEOI16_match) C++17
100 / 100
16 ms 11512 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;
typedef long long llong;
typedef pair<int, int> pii;

char S[100002];
int n;
int C[100001];
int P[100001][26];

void solve(int s, int e) {
    if (s > e) return;
    if (S[s] == S[e]) {
        S[s] = '(';
        S[e] = ')';
        solve(s + 1, e - 1);
        return;
    }
    int m = P[e][S[s] - 'a'];
    solve(s, m);
    solve(m + 1, e);
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> S + 1;
    n = strlen(S + 1);
    vector<char> st;
    for (int i = 1; i <= n; ++i) {
        if (!st.empty() && st.back() == S[i])
            st.pop_back();
        else st.push_back(S[i]);
    }
    if (!st.empty()) {
        printf("-1\n");
        return 0;
    }
    for (int i = 2; i <= n; ++i) {
        int c = S[i] - 'a';
        if (S[i - 1] == S[i]) C[i] = i - 1;
        else C[i] = P[i - 1][c];
        if (C[i] == 0) continue;
        for (int j = 0; j < 26; ++j) {
            if (j == c) continue;
            if (S[C[i] - 1] == 'a' + j) P[i][j] = C[i] - 1;
            else P[i][j] = P[C[i] - 1][j];
        }
    }
    solve(1, n);
    printf("%s\n", S + 1);
    return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:30:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     cin >> S + 1;
            ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 3 ms 1088 KB Output is correct
9 Correct 3 ms 1144 KB Output is correct
10 Correct 3 ms 1144 KB Output is correct
11 Correct 3 ms 1144 KB Output is correct
12 Correct 11 ms 6904 KB Output is correct
13 Correct 10 ms 7164 KB Output is correct
14 Correct 16 ms 8188 KB Output is correct
15 Correct 12 ms 9104 KB Output is correct
16 Correct 12 ms 9080 KB Output is correct
17 Correct 14 ms 9592 KB Output is correct
18 Correct 15 ms 10104 KB Output is correct
19 Correct 16 ms 10872 KB Output is correct
20 Correct 11 ms 7032 KB Output is correct
21 Correct 16 ms 11512 KB Output is correct