UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196953#3440. 括号X_X1001101ms39188kbC++11804b2023-11-04 09:52:162023-11-04 13:01:55

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=4e3+3,MOD=998244353;
int T,n,l[N],r[N],f[N][N];
char s[N];
void upd(int &x,int y){
	x+=y;
	if(x>=MOD) x-=MOD;
}
int main()
{
    scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
	    	scanf("%s",s);
	    	l[i]=r[i]=0;
	    	for(int j=0;j<strlen(s);j++){
	    		if(s[j]=='(') l[i]++;
	    		else{
	    			if(l[i]) l[i]--;
	    			else r[i]++;
				}
			}
    	}
    	int m=0;f[0][0]=1;
    	for(int i=1;i<=n;i++){
    		for(int j=0;j<=m+l[i];j++) f[i][j]=0;
    	    for(int j=0;j<=m;j++){
    	        upd(f[i][j],f[i-1][j]);
    	        if(r[i]<=j) upd(f[i][j-r[i]+l[i]],f[i-1][j]);
    	    }
    	    m+=l[i];
    	}
    	printf("%d\n",f[n][0]);
	}
	return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1240kb

input:

5
10
()((()((())))())()()(())()()()(()(()))()(((()(()((((())))))))())(((()(()((()))))(()))(()()(())(...

output:

4
2
8
8
4

result:

ok 5 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 1240kb

input:

5
10
()((((()((((()))())))(())))()(()))(())()((((()())()())(()()()((())((()))()(((())))((())()))())(...

output:

16
2
2
1
4

result:

ok 5 lines

Test #3:

score: 10
Accepted
time: 1ms
memory: 1240kb

input:

5
10
()(()(()))()()()()()()(())(()(()((((()()((()))))((()))((())((()))())))))()((()(())()((())())))(...

output:

2
4
1
1
4

result:

ok 5 lines

Test #4:

score: 10
Accepted
time: 170ms
memory: 38900kb

input:

5
3996
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(...

output:

603321095
448173631
393701954
914472959
762538865

result:

ok 5 lines

Test #5:

score: 10
Accepted
time: 182ms
memory: 38884kb

input:

5
3997
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(...

output:

528894773
509273114
226219996
829149959
949861738

result:

ok 5 lines

Test #6:

score: 10
Accepted
time: 174ms
memory: 38984kb

input:

5
3996
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(...

output:

468794090
809932458
601523255
88872389
655494177

result:

ok 5 lines

Test #7:

score: 10
Accepted
time: 124ms
memory: 38372kb

input:

5
2633
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(...

output:

538366364
527886537
501362852
60040777
277351770

result:

ok 5 lines

Test #8:

score: 10
Accepted
time: 144ms
memory: 39188kb

input:

5
2616
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(...

output:

715733558
894279974
307698611
108520503
441883588

result:

ok 5 lines

Test #9:

score: 10
Accepted
time: 158ms
memory: 39104kb

input:

5
2622
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(...

output:

315075720
609849364
474235717
449851958
472847756

result:

ok 5 lines

Test #10:

score: 10
Accepted
time: 148ms
memory: 39116kb

input:

5
2642
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(...

output:

124442224
505965987
545541192
207440322
136942205

result:

ok 5 lines

Extra Test:

score: 0
Extra Test Passed