QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#427139 | #1769. 括号序列 | xhgua | 100 ✓ | 312ms | 11964kb | C++14 | 1.8kb | 2024-06-01 10:17:44 | 2024-06-01 10:17:45 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 5e2 + 5, P = 1e9 + 7;
int n, k;
std::string s;
i64 dp[N][N][6];
bool isStar(int p) { return s[p] == '*' || s[p] == '?'; }
bool isPair(int l, int r) { return (s[l] == '(' || s[l] == '?') && (s[r] == ')' || s[r] == '?'); }
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> k >> s; s = " " + s;
for (int i = 1; i <= n; i++) if (isStar(i)) dp[i][i][0] = dp[i][i][5] = 1;
for (int i = 1; i < n; i++) {
if (isPair(i, i + 1)) dp[i][i + 1][1] = dp[i][i + 1][3] = 1;
if (k >= 2 && isStar(i) && isStar(i + 1)) dp[i][i + 1][0] = dp[i][i + 1][5] = 1;
}
for (int len = 3; len <= n; len++)
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
if (len <= k) {
dp[l][r][0] = 1;
for (int i = l; i <= r; i++) if (!isStar(i)) {
dp[l][r][0] = 0;
break;
}
}
if (isPair(l, r)) dp[l][r][1] = (dp[l + 1][r - 1][0] + dp[l + 1][r - 1][2] + dp[l + 1][r - 1][3] + dp[l + 1][r - 1][4]) % P;
dp[l][r][3] = dp[l][r][1];
dp[l][r][5] = dp[l][r][0];
for (int i = l; i < r; i++) {
dp[l][r][2] = (dp[l][r][2] + dp[l][i][3] * dp[i + 1][r][0] % P) % P;
dp[l][r][3] = (dp[l][r][3] + (dp[l][i][2] + dp[l][i][3]) * dp[i + 1][r][1] % P) % P;
dp[l][r][4] = (dp[l][r][4] + (dp[l][i][4] + dp[l][i][5]) * dp[i + 1][r][1] % P) % P;
dp[l][r][5] = (dp[l][r][5] + dp[l][i][4] * dp[i + 1][r][0] % P) % P;
}
}
std::cout << dp[1][n][3] << "\n";
return 0;
}
詳細信息
Test #1:
score: 5
Accepted
time: 1ms
memory: 3708kb
input:
14 3 ???*?(*????)??
output:
600
result:
ok single line: '600'
Test #2:
score: 5
Accepted
time: 1ms
memory: 3944kb
input:
15 7 ?()??)???*?????
output:
1443
result:
ok single line: '1443'
Test #3:
score: 5
Accepted
time: 1ms
memory: 5692kb
input:
15 10 ??*??????????(?
output:
7801
result:
ok single line: '7801'
Test #4:
score: 5
Accepted
time: 1ms
memory: 5852kb
input:
38 6 ?(????(??*??????*????*????(?(*(??*????
output:
804755392
result:
ok single line: '804755392'
Test #5:
score: 5
Accepted
time: 0ms
memory: 3832kb
input:
39 17 ?????*???)????(????*????????????*???)??
output:
734493810
result:
ok single line: '734493810'
Test #6:
score: 5
Accepted
time: 1ms
memory: 5648kb
input:
40 2 ?*???(???(?*???))??*????*???*??**(???(??
output:
946388230
result:
ok single line: '946388230'
Test #7:
score: 5
Accepted
time: 1ms
memory: 5820kb
input:
40 11 ?????*????????)??(??)?(??????*??????????
output:
513756777
result:
ok single line: '513756777'
Test #8:
score: 5
Accepted
time: 0ms
memory: 5792kb
input:
40 26 ????)????????*????????????*?????????()??
output:
156637450
result:
ok single line: '156637450'
Test #9:
score: 5
Accepted
time: 2ms
memory: 5676kb
input:
95 3 ???)??????????)?????????*???????????(?(???????????()?????(???????**??????(??*??????????***?????
output:
267516104
result:
ok single line: '267516104'
Test #10:
score: 5
Accepted
time: 2ms
memory: 5924kb
input:
98 22 ?*??????*???)??????(??)?)????????*??????????(?????))??????*????????????*****?????***????(???????*?
output:
641483452
result:
ok single line: '641483452'
Test #11:
score: 5
Accepted
time: 2ms
memory: 5964kb
input:
99 15 ???*????*??)??)??????(????**????(???*??????*??????*???)????*???????**?????????*????????*?*??????)??
output:
195466063
result:
ok single line: '195466063'
Test #12:
score: 5
Accepted
time: 3ms
memory: 6408kb
input:
100 17 ???*????(???????*?????(??(??????*???????*???????*?????)??)?????(??????*??????(????*????*???*)????*??
output:
695865604
result:
ok single line: '695865604'
Test #13:
score: 5
Accepted
time: 3ms
memory: 6172kb
input:
100 33 ??(???*????)??)????)????????????*?????**????????????*???????????*????????*?*???????))???????*?????)?
output:
114367997
result:
ok single line: '114367997'
Test #14:
score: 5
Accepted
time: 280ms
memory: 11872kb
input:
497 238 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
output:
211791029
result:
ok single line: '211791029'
Test #15:
score: 5
Accepted
time: 309ms
memory: 11488kb
input:
500 463 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
output:
572342110
result:
ok single line: '572342110'
Test #16:
score: 5
Accepted
time: 273ms
memory: 11680kb
input:
491 88 ???????????*?????????????*??????*?????*?????***?????*?????????*?????(??**??????????????*??***????????????????*?????????)*????????????????(*????????????????*?*????????????(??????*???????????*??????????*????*?*????????*???????????*?????*??????????*???????*??????**???????*???*???????????????????...
output:
617614760
result:
ok single line: '617614760'
Test #17:
score: 5
Accepted
time: 312ms
memory: 11480kb
input:
498 135 ?*?????????????*?????*??*??????*?????????**?*??*?**??*????*?????????????????????????????????*????**???????????*????*????*????*?????*????????????(()?*????????*???(?*???????????????*????????????*?????????????????????????(??*??(????)???*????)????)*???????????*???????(???????**????????????*?????...
output:
637064892
result:
ok single line: '637064892'
Test #18:
score: 5
Accepted
time: 280ms
memory: 11964kb
input:
500 174 ??*????(((*???????????????????????*???*???????*???*?????????????*??*?*??**?????(???(??????????????????*)?????????????(????????*????*????*????????????????????*?????????*??????*???*??????????????*?????????????????*???*???????????????????????????????????*?*????????????????????????????*??*??????...
output:
835939342
result:
ok single line: '835939342'
Test #19:
score: 5
Accepted
time: 307ms
memory: 11300kb
input:
500 221 ????????????*?*??*?????????*???*??????*(???)???)????(?*?????(??**??*???*??????????*??*?*???????*??????*???????*?????)????*???)??)??*??????????????????*???*???????????????????*????????**????????*?????????*??????*??*?*??????????????????*?*????????*???*??????????*????????????*???????????????*??...
output:
977731606
result:
ok single line: '977731606'
Test #20:
score: 5
Accepted
time: 299ms
memory: 11752kb
input:
500 316 ???*?*?????*???*???(??????????????*????(?????????????????*?????????????????????*???????????*??**??????*????????????????*????)???*????????????*?*????????????????????????*?*??*?????????????????????*?????????**?*???**???*????*?????????????????????????????????????*???????*??????????*?**????????*...
output:
875239106
result:
ok single line: '875239106'
Extra Test:
score: 0
Extra Test Passed