Submission #341627

# Submission time Handle Problem Language Result Execution time Memory
341627 2020-12-30T09:51:00 Z phathnv Match (CEOI16_match) C++11
0 / 100
1 ms 364 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 1;
const int AL = 26;

int n, match[N], pre[N][AL], answer[N];
string s;

void readInput(){
    cin >> s;
    n = s.size();
    s = '*' + s;
}

void process(int l, int r){
    if (l > r)
        return;
    int best = pre[r][s[l] - 'a'];
    if (match[l] == best){
        answer[l] = 1;
        answer[best] = 2;
        process(l + 1, best - 1);
    } else {
        int a = match[l];
        int b = match[best];
        answer[l] = 1;
        answer[match[l]] = 1;
        answer[match[best]] = 2;
        answer[best] = 2;
        process(l + 1, match[l] - 1);
        process(match[l] + 1, match[best] - 1);
        process(match[best] + 1, best - 1);
    }
    process(best + 1, r);
}

void solve(){
    stack <int> st;
    for(int i = 1; i <= n; i++){
        if (st.empty()){
            st.push(i);
        } else if (s[st.top()] == s[i]){
            match[st.top()] = i;
            match[i] = st.top();
            st.pop();
            for(int j = 0; j < AL; j++)
                pre[i][j] = pre[match[i] - 1][j];
            pre[i][s[i] - 'a'] = i;
        } else {
            st.push(i);
        }
    }
    if (!st.empty()){
        cout << -1;
        return;
    }

    process(1, n);

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

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    readInput();
    solve();
    return 0;
}

Compilation message

match.cpp: In function 'void process(int, int)':
match.cpp:28:13: warning: unused variable 'a' [-Wunused-variable]
   28 |         int a = match[l];
      |             ^
match.cpp:29:13: warning: unused variable 'b' [-Wunused-variable]
   29 |         int b = match[best];
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -