답안 #131584

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
131584 2019-07-17T10:02:17 Z johutha 괄호 문자열 (CEOI16_match) C++14
0 / 100
2 ms 376 KB
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

#define int int64_t

using namespace std;

struct segtree
{
    vector<int> table;
    int n;

    void update(int k, int v, int l, int r, int pos)
    {
        if (l == r)
        {
            table[pos] = v;
            return;
        }
        if (k <= (l + r)/2) update(k, v, l, (l + r)/2, 2*pos + 1);
        else update(k, v, (l + r)/2 + 1, r, 2*pos + 2);
        table[pos] = table[2*pos + 1] + table[2*pos + 2];
    }

    void update(int k, int v)
    {
        update(k, v, 0, n - 1, 0);
    }

    int query(int ql, int qr, int l, int r, int pos)
    {
        if (ql <= l && r <= qr) return table[pos];
        if (ql > r || qr < l) return 0;
        return query(ql, qr, l, (l + r)/2, 2*pos + 1) + query(ql, qr, (l + r)/2 + 1, r, 2*pos + 2);
    }

    int query(int qr, int ql)
    {
        return query(ql, qr, 0, n - 1, 0);
    }

    void build(vector<int> &vals, int l, int r, int pos)
    {
        if (l == r)
        {
            table[pos] = vals[l];
            return;
        }
        build(vals, l, (l + r)/2, 2*pos + 1);
        build(vals, (l + r)/2 + 1, r, 2*pos + 2);
        table[pos] = table[2*pos + 1] + table[2*pos + 2];
    }
};

struct brackseq
{
    string s;
    
    int n;

    segtree st;
    vector<int> corr;

    bool check(int l, int r)
    {
        if (r < l) return false;
        if (st.query(l, l) == 1 || st.query(r, r) == -1) return false;
        if (l + 1 != r && st.query(l, r) != 0) return false;
        if (st.query(corr[l], corr[r]) != 0) return false;
        return true;
    }

    string calc()
    {
        vector<int> stack;
        vector<int> br(n, 0);
        corr.resize(n);
        string res;

        for (int i = 0; i < n; i++)
        {
            if (stack.size() == 0 || s[i] != s[stack.back()])
            {
                stack.push_back(i);
                br[i] = 1;
            }
            else
            {
                corr[stack.back()] = i;
                corr[i] = stack.back();
                stack.pop_back();
                br[i] = -1;
            }
        }

        st.n = n;
        st.table.resize(4*n);
        st.build(br, 0, n - 1, 0);

        vector<vector<int>> chlist(256);

        for (int i = n - 1; i >= 0; i--)
        {
            chlist[s[i]].push_back(i);
        }

        if (!stack.empty()) return "-1";

        for (int i = 0; i < 255; i++)
        {
            reverse(chlist[i].begin(), chlist[i].end());
        }

        for (int i = 0; i < n; i++)
        {
            for (int ot : chlist[s[i]])
                if (check(i, ot))
                {
                    st.update(i, 1);
                    st.update(ot, -1);
                    corr[corr[ot]] = corr[i];
                    corr[corr[i]] = corr[ot];
                    corr[i] = ot;
                    corr[ot] = i;
                    break;
                }  
        }
        for (int i = 0; i < n; i++)
        {
            if (st.query(i, i) == 1) res.push_back('(');
            else res.push_back(')');
        }
        return res;
    }
};

signed main()
{
    brackseq bs;
    cin >> bs.s;
    bs.n = bs.s.size();
    cout << bs.calc() << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 256 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 256 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 256 KB Output isn't correct
3 Halted 0 ms 0 KB -