QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#323393#4824. Bracket-and-bar Sequenceshotboy27030 1ms3856kbC++144.2kb2024-02-09 15:07:032024-02-09 15:07:03

Judging History

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

  • [2024-02-09 15:07:03]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3856kb
  • [2024-02-09 15:07:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1LL)
#define MASK(i) (1LL << (i))
ull dp[60];
ll wow[61];
ll recursion_count = 0;
ll find_right(string s){
    assert(sz(s)>0);
    ll sum = 0;
    ll l = -1;
    for (ll i = sz(s) - 2;i >= 0;i --){
        sum += (s[i] == '|' ? 0 : (s[i] == '(' ? 1 : -1));
        if (sum == 0 && s[i] == '|'){l = i;break;}
    }
    return l;
}
ll normal_to_int(string s);
ll wow_to_int(string s){
    if ((recursion_count++)>1000){
        assert(0);
        return 0;
    }
    ll n=sz(s)/3;
    assert(n!=0);
    if (n < 2)return 0;
    ll l = find_right(s);
    ll right_sz = (sz(s) - 1 - (l + 1) + 1)/3;
    ll res = 0;
    for (ll i = 0;i < right_sz;i ++){
        res += dp[i] * dp[n-1-i];
    }
    string left_part(s,1,l-1),right_part(s,l+1,sz(s)-2-l);
    return res + normal_to_int(left_part) * dp[sz(right_part)] + normal_to_int(right_part);
}
ll normal_to_int(string s){
    if ((recursion_count++)>1000){
        assert(0);
        return 0;
    }
    ll n = sz(s)/3;
    if (n < 2)return 0;
    ll sum = 0;
    ll l = -1;
    for (ll i = sz(s) - 1;i >= 0;i --){
        sum += (s[i] == '|' ? 0 : (s[i] == '(' ? 1 : -1));
        if (sum == 0){l = i;break;}
    }
    ll right_sz = (sz(s)-l)/3;
    if (right_sz==n){
        return wow_to_int(s);
    }
    else{
        ll res = wow[n];
        for (ll i = 1;i < right_sz;i ++){
            res += wow[i] * dp[n-i];
        }
        string left_part(s,0,sz(s)-right_sz*3),right_part(s,sz(s)-right_sz*3,right_sz*3);
//        cout<<s<<' '<<left_part<<' '<<right_part<<endl;
        return res + normal_to_int(left_part) * wow[sz(right_part)/3] + wow_to_int(right_part);
    }
}
ll to_int(string s){
    recursion_count = 0;
    ll res = 0;
    ll n=sz(s)/3;
    for (ll i = 0;i < n;i++)res+=dp[i];
    res += normal_to_int(s);
    return res;
}
string pr_to_string(ll n,ll k){
    if (n == 1)return "(|)";
    if (n==0)return "";
    if (k < wow[n]){
        for (ll j = 0;j < n;j ++){
            if (dp[j] * dp[n-1-j]<=k)k-=dp[j] * dp[n-1-j];
            else{
                ll k1,k2;
                k1 = k / dp[j];
                k2 = k % dp[j];
                return "(" + pr_to_string(n-1-j,k1) + "|" +  pr_to_string(j,k2) + ")";
            }
        }
    }
    else{
        k -= wow[n];
        for (ll i = 1;i < n;i ++){
            if (wow[i] * dp[n-i]<=k)k-=wow[i] * dp[n-i];
            else{
                ll k1,k2;
                k1 = k / wow[i];
                k2 = k % wow[i];
                return pr_to_string(n-i,k1)+pr_to_string(i,k2);
            }
        }
    }

}
string too_string(ll x){
    recursion_count = 0;
    for (ll i = 0;i <= 25;i ++){
        if (x >= ll(dp[i]))x -= dp[i];
        else {return pr_to_string(i,x);}
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
    dp[0] = 1;
    for (ll i = 1;i <= 25;i ++){
        wow[i] = 0;
        for (ll j = 0;j < i;j ++){
            wow[i] += dp[j] * dp[i-1-j];
        }
        dp[i] = wow[i];
        for (ll j = 1;j < i;j ++){
            dp[i] += wow[j] * dp[i-j];
        }
//        cout<<i<<' '<<ld(dp[i])<<' '<<ld(wow[i])<<'\n';
    }
//    for (ll i = 0;i < dp[0] + dp[1] + dp[2] + dp[3] + dp[4] + dp[5];i ++){
//        cout<<i<<' '<<too_string(i)<<' '<<to_int(too_string(i))<<endl;
//    }
//    cout<<normal_to_int("(|)(|)")<<endl;
    string task;
    cin>>task;
    ll t;
    cin>>t;
    while (t--){
        ll n;
        cin>>n;
        if (task=="encode"){
            string s;
            cin>>s;
            cout<<to_int(s)<<'\n';
//            cout<<ld(to_int(s))<<'\n';
//            if (recursion_count > 1000){
//                cout<<s<<'\n';
//            }
//            cout<<pr_to_int(s)<<'\n';
        }
        else{
            ll x;
            cin>>x;
            cout<<too_string(x)<<'\n';
        }
    }
}
/*
encode
1
4
(|(|)(|)(|))
decode
3
1
1
4
17
5
302

1274049
1273964
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3652kb

input:

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

output:

1
17
302

input:

decode
3
1
1
4
17
5
302

output:

(|)
((((|)|)|)|)
(|(|))((|(|))|)

result:

ok 3 lines

Test #2:

score: 100
Accepted
time: 0ms
memory: 3812kb

input:

encode
1
1
(|)

output:

1

input:

decode
1
1
1

output:

(|)

result:

ok single line: '(|)'

Test #3:

score: 100
Accepted
time: 0ms
memory: 3852kb

input:

encode
3
2
((|)|)
1
(|)
2
(|(|))

output:

2
1
3

input:

decode
3
2
2
1
1
2
3

output:

((|)|)
(|)
(|(|))

result:

ok 3 lines

Test #4:

score: 0
Wrong Answer
time: 1ms
memory: 3856kb

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:

14
16
15
13
11
10
9
12
8
7
6
5
56
64
63
58
71
70
69
57
68
67
66
65
55
62
61
53
52
51
44
46
45
43
41
40
39
42
38
37
36
35
54
60
59
50
34
33
32
49
48
47
53
41
29
26
28
27
25
23
22
21
24
20
19
18
17
254
289
288
262
314
313
312
261
311
310
309
308
256
293
292
269
268
267
342
344
343
341
339
338
337
340
...

input:

decode
1000
3
14
3
16
3
15
3
13
3
11
3
10
3
9
3
12
3
8
3
7
3
6
3
5
4
56
4
64
4
63
4
58
4
71
4
70
4
69
4
57
4
68
4
67
4
66
4
65
4
55
4
62
4
61
4
53
4
52
4
51
4
44
4
46
4
45
4
43
4
41
4
40
4
39
4
42
4
38
4
37
4
36
4
35
4
54
4
60
4
59
4
50
4
34
4
33
4
32
4
49
4
48
4
47
4
53
4
41
4
29
4
26
4
28
4
27
4
2...

output:

(|)(|)(|)
(|)(|(|))
(|)((|)|)
(|(|))(|)
(|(|)(|))
(|(|(|)))
(|((|)|))
((|)|)(|)
((|)|(|))
((|)(|)|)
((|(|))|)
(((|)|)|)
(|)(|)(|)(|)
(|)(|)(|(|))
(|)(|)((|)|)
(|)(|(|))(|)
(|)(|(|)(|))
(|)(|(|(|)))
(|)(|((|)|))
(|)((|)|)(|)
(|)((|)|(|))
(|)((|)(|)|)
(|)((|(|))|)
(|)(((|)|)|)
(|(|))(|)(|)
(|(|))(|(|)...

result:

wrong answer 53rd lines differ - expected: '((|)(|)|(|))', found: '(|(|)(|))(|)'