QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#294637 | #4824. Bracket-and-bar Sequences | ucup-team133# | 0 | 1ms | 3776kb | C++23 | 3.5kb | 2023-12-30 15:12:52 | 2023-12-30 15:12:53 |
answer
#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif
using namespace std;
typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
template <class T> istream& operator>>(istream& is, vector<T>& v) {
for (auto& x : v) is >> x;
return is;
}
template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
auto sep = "";
for (const auto& x : v) os << exchange(sep, " ") << x;
return os;
}
template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }
template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }
template <class T> void mkuni(vector<T>& v) {
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
}
template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }
const int MAX = 25;
ll dp[MAX + 1][3]; // 0: 最後が concatenation, 1: 最後が (|), 2: 合計
void precalc() {
dp[0][2] = 1;
for (int i = 1; i <= MAX; i++) {
for (int j = 1; j < i; j++) dp[i][0] += dp[j][1] * dp[i - j][2];
for (int j = 0; j < i; j++) dp[i][1] += dp[j][2] * dp[i - 1 - j][2];
dp[i][2] = dp[i][0] + dp[i][1];
}
}
ll encode(int n, const string& S) {
if (n == 0) return 0;
assert(int(S.size()) == 3 * n);
assert(S[0] == '(');
ll res = 0;
for (int i = 1, sum = 0; i < n; i++) {
for (int j = 3 * (i - 1); j < 3 * i; j++) {
sum += (S[j] == '(' ? 1 : S[j] == ')' ? -1 : 0);
}
if (sum == 0) {
ll pre = encode(i, S.substr(0, 3 * i)), suf = encode(n - i, S.substr(3 * i, 3 * (n - i)));
return res + (pre * dp[n - i][2] + suf);
}
res += dp[i][1] * dp[n - i][2];
}
assert(res == dp[n][0]);
string T = S.substr(1, 3 * n - 2);
for (int i = 0, sum = 0; i < n; i++) {
if (sum == 0 and T[3 * i] == '|') {
ll pre = encode(i, T.substr(0, 3 * i)), suf = encode(n - 1 - i, T.substr(3 * i + 1, 3 * (n - 1 - i)));
return res + (pre * dp[n - 1 - i][2] + suf);
}
res += dp[i][2] * dp[n - 1 - i][2];
for (int j = 3 * i; j < 3 * (i + 1); j++) {
sum += (T[j] == '(' ? 1 : T[j] == ')' ? -1 : 0);
}
}
assert(false);
}
string decode(int n, ll x) {
if (n == 0) return "";
for (int i = 1; i < n; i++) {
ll nxt = dp[i][1] * dp[n - i][2];
if (x < nxt) {
ll pre = x / dp[n - i][2], suf = x % dp[n - i][2];
return decode(i, pre) + decode(n - i, suf);
}
x -= nxt;
}
for (int i = 0; i < n; i++) {
ll nxt = dp[i][2] * dp[n - 1 - i][2];
if (x < nxt) {
ll pre = x / dp[n - 1 - i][2], suf = x % dp[n - 1 - i][2];
return "(" + decode(i, pre) + "|" + decode(n - 1 - i, suf) + ")";
}
x -= nxt;
}
assert(false);
}
void solve1() {
int n;
string S;
cin >> n >> S;
cout << encode(n, S) << '\n';
}
void solve2() {
int n;
ll x;
cin >> n >> x;
cout << decode(n, x) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
precalc();
string op;
int t;
cin >> op >> t;
for (; t--;) {
if (op == "encode")
solve1();
else
solve2();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
input:
encode 3 1 (|) 4 ((((|)|)|)|) 5 (|(|))((|(|))|)
output:
0 54 77
input:
decode 3 1 0 4 54 5 77
output:
(|) ((((|)|)|)|) (|(|))((|(|))|)
result:
ok 3 lines
Test #2:
score: 100
Accepted
time: 0ms
memory: 3776kb
input:
encode 1 1 (|)
output:
0
input:
decode 1 1 0
output:
(|)
result:
ok single line: '(|)'
Test #3:
score: 100
Accepted
time: 0ms
memory: 3648kb
input:
encode 3 2 ((|)|) 1 (|) 2 (|(|))
output:
2 0 1
input:
decode 3 2 2 1 0 2 1
output:
((|)|) (|) (|(|))
result:
ok 3 lines
Test #4:
score: 0
Wrong Answer
time: 1ms
memory: 3616kb
input:
encode 1000 3 (|)(|)(|) 3 (|)(|(|)) 3 (|)((|)|) 3 (|(|))(|) 3 (|(|)(|)) 3 (|(|(|))) 3 (|((|)|)) 3 ((|)|)(|) 3 ((|)|(|)) 3 ((|)(|)|) 3 ((|(|))|) 3 (((|)|)|) 4 (|)(|)(|)(|) 4 (|)(|)(|(|)) 4 (|)(|)((|)|) 4 (|)(|(|))(|) 4 (|)(|(|)(|)) 4 (|)(|(|(|))) 4 (|)(|((|)|)) 4 (|)((|)|)(|) 4 (|)((|)|(|)) 4 (|)((|)...
output:
0 1 2 4 5 6 7 5 8 9 10 11 0 1 2 4 5 6 7 5 8 9 10 11 15 16 17 23 24 25 25 26 27 29 30 31 32 30 33 34 35 36 18 19 20 26 37 38 39 27 28 29 40 41 42 43 44 45 47 48 49 50 48 51 52 53 54 0 1 2 4 5 6 7 5 8 9 10 11 15 16 17 23 24 25 25 26 27 29 30 31 32 30 33 34 35 36 18 19 20 26 37 38 39 27 28 29 40 41 42 ...
input:
decode 1000 3 0 3 1 3 2 3 4 3 5 3 6 3 7 3 5 3 8 3 9 3 10 3 11 4 0 4 1 4 2 4 4 4 5 4 6 4 7 4 5 4 8 4 9 4 10 4 11 4 15 4 16 4 17 4 23 4 24 4 25 4 25 4 26 4 27 4 29 4 30 4 31 4 32 4 30 4 33 4 34 4 35 4 36 4 18 4 19 4 20 4 26 4 37 4 38 4 39 4 27 4 28 4 29 4 40 4 41 4 42 4 43 4 44 4 45 4 47 4 48 4 49 4 5...
output:
(|)(|)(|) (|)(|(|)) (|)((|)|) (|(|))(|) (|(|)(|)) (|(|(|))) (|((|)|)) (|(|)(|)) ((|)|(|)) ((|)(|)|) ((|(|))|) (((|)|)|) (|)(|)(|)(|) (|)(|)(|(|)) (|)(|)((|)|) (|)(|(|))(|) (|)(|(|)(|)) (|)(|(|(|))) (|)(|((|)|)) (|)(|(|)(|)) (|)((|)|(|)) (|)((|)(|)|) (|)((|(|))|) (|)(((|)|)|) (|(|))(|)(|) (|(|))(|(|)...
result:
wrong answer 8th lines differ - expected: '((|)|)(|)', found: '(|(|)(|))'