QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#618170#4824. Bracket-and-bar Sequenceslichenyu_ac0 2ms3992kbC++141.8kb2024-10-06 19:32:042024-10-06 19:32:05

Judging History

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

  • [2024-10-06 19:32:05]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:3992kb
  • [2024-10-06 19:32:04]
  • 提交

answer

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=110;

namespace encode{

	ll f[N];
	int n;
	string s;

	ll solve(int n,string s){
		if(!n)return 0;
		int l=0,mid=0,r=0;
		int top=0;
		for(int i=0;;i++){
			top+=s[i]=='(',top-=s[i]==')';
			if(top==1&&s[i]=='|')mid=i;
			if(!top){
				r=i;
				break;
			}
		}
		int cnt1=(mid-l-1)/3,cnt2=(r-mid-1)/3,cnt3=(n*3-r-1)/3;
		ll val=0;
		for(int i=0;i<n;i++)
			for(int j=0;i+j<n;j++)
				if(i==cnt1&&j==cnt2)
					return val+solve(cnt1,s.substr(1,cnt1*3))*f[cnt2]+solve(cnt2,s.substr(mid+1,cnt2*3))*f[cnt3]+solve(cnt3,s.substr(r+1,cnt3*3));
				else val+=f[i]*f[j]*f[n-1-i-j];
		return -1;
	}

	void solve(){
		cin>>n>>s;
		printf("%lld\n",solve(n,s));
	}

	int main(){
		f[0]=1;
		for(int i=1;i<=25;i++)
			for(int j=0;j<i;j++)
				for(int k=0;j+k<i;k++)
					f[i]+=f[j]*f[k]*f[i-1-j-k];
		int T;
		cin>>T;
		while(T--)solve();
		return 0;
	}
}

namespace decode{

	ll f[N];
	int n;
	string s;

	string solve(int n,ll s){
		if(!n)return "";
		for(int i=0;i<n;i++)
			for(int j=0;i+j<n;j++)
				if(s<f[i]*f[j]*f[n-1-i-j]){
					ll s3=s%f[n-1-i-j];
					ll s2=(s/f[n-1-i-j])%f[j];
					ll s1=s/f[n-1-i-j]/f[j];
					return "("+solve(i,s1)+"|"+solve(j,s2)+")"+solve(n-1-i-j,s3);
				}else s-=f[i]*f[j]*f[n-1-i-j];
		return "-1";
	}

	void solve(){
		int n;ll s;
		cin>>n>>s;
		cout<<solve(n,s)<<endl;
	}

	int main(){
		f[0]=1;
		for(int i=1;i<=25;i++)
			for(int j=0;j<i;j++)
				for(int k=0;j+k<i;k++)
					f[i]+=f[j]*f[k]*f[i-1-j-k];
		int T;
		cin>>T;
		while(T--)solve();
		return 0;
	}
}

int main(){
    // freopen("test.in","r",stdin);
    // freopen("test.out","w",stdout);
    string s;
    cin>>s;
    if(s=="encode")encode::main();
    else decode::main();
    return 0;
}

詳細信息

Test #1:

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

input:

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

output:

0
54
65

input:

decode
3
1
0
4
54
5
65

output:

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

result:

ok 3 lines

Test #2:

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

input:

encode
1
1
(|)

output:

0

input:

decode
1
1
0

output:

(|)

result:

ok single line: '(|)'

Test #3:

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

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: 2ms
memory: 3716kb

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
3
4
5
6
7
8
9
10
11
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
...

input:

decode
1000
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
3
10
3
11
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
4
10
4
11
4
12
4
13
4
14
4
15
4
16
4
17
4
18
4
19
4
20
4
21
4
22
4
23
4
24
4
25
4
26
4
27
4
28
4
29
4
30
4
31
4
32
4
33
4
34
4
35
4
36
4
37
4
38
4
39
4
40
4
41
4
42
4
43
4
44
4
45
4
46
4
47
4
48
4
4...

output:

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

result:

wrong answer 244th lines differ - expected: '((|(|))|)(|)(|)', found: '((|)(|)|)(|(|))'