UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196958#3440. 括号smwtcat1001267ms1540kbC++111.2kb2023-11-04 10:46:392023-11-04 13:02:39

answer

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int Mod = 998244353;
inline void uadd(int &a, int b){ a += b-Mod; a += (a>>31) & Mod; }
inline void usub(int &a, int b){ a -= b, a += (a>>31) & Mod; }
inline void umul(int &a, int b){ a = (int)(1ll * a * b % Mod); }
inline int add(int a, int b){ a += b-Mod; a += (a>>31) & Mod; return a; }
inline int sub(int a, int b){ a -= b, a += (a>>31) & Mod; return a; }
inline int mul(int a, int b){ return (int)(1ll * a * b % Mod); }

int n;
string s[4040];
int a[4040], b[4040];
int dp[4040];
void solve(){
	cin >> n;
	rep(i, n) cin >> s[i];
	rep(i, n){
		int nv = a[i] = 0;
		rep(j, (int)s[i].size()){
			nv += (s[i][j] == '(' ? 1 : -1);
			a[i] = min(a[i], nv);
		}
		b[i] = nv;
	}
	memset(dp, 0, sizeof(dp));
	dp[0] = 1;
	rep(i, n){
		static int tmp[4040];
		memset(tmp, 0, sizeof(tmp));
		for(int j = 4004 - max(b[i], 0); j >= -a[i]; --j)
			uadd(tmp[j + b[i]], dp[j]);
		rep(j, 4005)
			uadd(dp[j], tmp[j]);
	}
	cout << dp[0] << "\n";
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int T;
	cin >> T;
	while(T--) solve();

	return 0;
}

详细

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

Test #1:

score: 10
Accepted
time: 3ms
memory: 1332kb

input:

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

output:

4
2
8
8
4

result:

ok 5 lines

Test #2:

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

input:

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

output:

16
2
2
1
4

result:

ok 5 lines

Test #3:

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

input:

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

output:

2
4
1
1
4

result:

ok 5 lines

Test #4:

score: 10
Accepted
time: 295ms
memory: 1540kb

input:

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

output:

603321095
448173631
393701954
914472959
762538865

result:

ok 5 lines

Test #5:

score: 10
Accepted
time: 187ms
memory: 1536kb

input:

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

output:

528894773
509273114
226219996
829149959
949861738

result:

ok 5 lines

Test #6:

score: 10
Accepted
time: 199ms
memory: 1536kb

input:

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

output:

468794090
809932458
601523255
88872389
655494177

result:

ok 5 lines

Test #7:

score: 10
Accepted
time: 147ms
memory: 1536kb

input:

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

output:

538366364
527886537
501362852
60040777
277351770

result:

ok 5 lines

Test #8:

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

input:

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

output:

715733558
894279974
307698611
108520503
441883588

result:

ok 5 lines

Test #9:

score: 10
Accepted
time: 146ms
memory: 1536kb

input:

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

output:

315075720
609849364
474235717
449851958
472847756

result:

ok 5 lines

Test #10:

score: 10
Accepted
time: 146ms
memory: 1540kb

input:

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

output:

124442224
505965987
545541192
207440322
136942205

result:

ok 5 lines

Extra Test:

score: 0
Extra Test Passed