QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#297361#4824. Bracket-and-bar Sequencesfxhd0 0ms0kbC++173.2kb2024-01-04 12:20:442024-01-04 12:20:44

Judging History

你现在查看的是最新测评结果

  • [2024-01-04 12:20:44]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-01-04 12:20:44]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG
  #include "debug.hpp"
#else
  #define dbg(...) 0
#endif

const int N = 25;
array<int64_t, N + 1> dp1, dp2;

void precalculate() {
  dp1[0] = 1;
  dp2[0] = 0;
  for (int n = 1; n <= N; ++n) {
    dp2[n] = 0;
    for (int i = 0; i < n; ++i) {
      dp2[n] += dp1[i] * dp1[n - 1 - i];
    }
    dp1[n] = 0;
    for (int i = 1; i <= n; ++i) {
      dp1[n] += dp2[i] * dp1[n - i];
    }
  }
}

int64_t encode(const string& s) {
  int n = s.size() / 3;
  vector<array<int, 2>> pos(3 * n, array<int, 2>{-1, -1});
  vector<array<int, 3>> st;
  for (int i = 0; i < (3 * n); ++i) {
    if (s[i] == '(') {
      st.push_back({i, -1});
    }
    else if (st.back()[1] < 0) {
      st.back()[1] = i;
    }
    else {
      pos[st.back()[0]] = {st.back()[1], i};
      st.pop_back();
    }
  }
  auto encode1 = [&](auto&& encode1_, auto&& encode2_, int l, int r) -> int64_t {
    if (l == r) return 0;
    int m = r - l;
    assert((m % 3) == 0);
    m /= 3;
    int r1 = pos[l][1] + 1;
    assert((r1 % 3) == (r % 3));
    int m1 = (r1 - l) / 3;
    int64_t x1 = encode2_(encode1_, encode2_, l + 1, pos[l][0], pos[l][1]);
    int64_t x2 = encode1_(encode1_, encode2_, r1, r);
    int64_t res = 0;
    for (int k = 1; k < m1; ++k) {
      res += dp2[k] * dp1[m - k];
    }
    return res + x1 * dp1[m - m1] + x2;
  };
  auto encode2 = [&](auto&& encode1_, auto&& encode2_, int l, int mid, int r) -> int64_t {
    int64_t x1 = encode1_(encode1_, encode2_, l, mid);
    int64_t x2 = encode1_(encode1_, encode2_, mid + 1, r);
    int m1 = mid - l;
    assert((m1 % 3) == 0);
    m1 /= 3;
    int m2 = r - mid - 1;
    assert((m2 % 3) == 0);
    m2 /= 3;
    assert((r - l) == (3 * (m1 + m2)));
    int64_t res = 0;
    for (int k = 0; k < m1; ++k) {
      res += dp1[k] * dp1[m1 + m2 - k];
    }
    return res + x1 * dp1[m2] + x2;
  };
  return encode1(encode1, encode2, 0, 3 * n);
}

string decode1(int n, int64_t x);

string decode2(int n, int64_t x) {
  int n1 = 0;
  while (x >= (dp1[n1] * dp1[n - 1 - n1])) {
    x -= dp1[n1] * dp1[n - 1 - n1];
    n1++;
  }
  string ans; ans.reserve(3 * n);
  ans.push_back('(');
  ans += decode1(n1, x / dp1[n - 1 - n1]);
  ans.push_back('|');
  ans += decode1(n - 1 - n1, x % dp1[n - 1 - n1]);
  ans.push_back(')');
  return ans;
}

string decode1(int n, int64_t x) {
  string ans; ans.reserve(3 * n);
  if (n == 0) return ans;
  int n1 = 1;
  while (x >= (dp2[n1] * dp1[n - n1])) {
    x -= dp2[n1] * dp1[n - n1];
    n1++;
  }
  ans += decode2(n1, x / dp1[n - n1]);
  ans += decode1(n - n1, x % dp1[n - n1]);
  return ans;
}

void test_case_encode() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  assert(int(s.size()) == (3 * n));
  cout << encode(s) << '\n';
}

void test_case_decode() {
  int n;
  cin >> n;
  int64_t x;
  cin >> x;
  string s = decode1(n, x);
  cout << s << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  precalculate();
  string s;
  cin >> s;
  int t;
  cin >> t;
  if (s == "encode") {
    while (t--) {
      test_case_encode();
    }
  }
  else {
    assert(s == "decode");
    while (t--) {
      test_case_decode();
    }
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Stage 1: Program answer Runtime Error

input:

encode
3
1
(|)
4
((((|)|)|)|)
5
(|(|))((|(|))|)

output:


input:


output:


result: