当前位置:网站首页>2022 Multi-school Second Session K Question Link with Bracket Sequence I
2022 Multi-school Second Session K Question Link with Bracket Sequence I
2022-08-05 00:21:00 【雨肯定】
题目链接
题目大意
Given us a length of n n n的括号序列,and tells us it is a length of m m mA substring of a valid parentheses sequence,Ask us that this length is m m mHow many cases of parenthesis sequences are there?
题解
考虑DP,设 f i , j , k f_{i,j,k} fi,j,k 表示考虑 b b b串的前i个字符,和 a a aThe longest common subsequence length of the string is j j j,左括号比右括号多 k k k,Each time the enumeration is shifted, whether it is an opening or closing parenthesis,最后答案即为 f [ m ] [ n ] [ 0 ] f_{[m][n][0]} f[m][n][0].
代码
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 210;
const int mod = 1e9 + 7;
int f[maxn][maxn][maxn];
int main()
{
int t; cin >> t;
while(t --)
{
memset(f, 0, sizeof f);
int n, m; cin >> n >> m;
string s; cin >> s;
s = ' ' + s;
f[0][0][0] = 1;
for(int i = 1; i <= m; i ++){
for(int j = 0; j <= n && j <= i; j ++){
for(int k = 0; k <= i; k ++){
if(k) {
if(j && s[j] == '(') f[i][j][k] = (f[i][j][k] + f[i - 1][j - 1][k - 1]) % mod;
else f[i][j][k] = (f[i][j][k] + f[i - 1][j][k - 1]) % mod;
}
if(k != m) {
if(!j || s[j] == '(') f[i][j][k] = (f[i][j][k] + f[i - 1][j][k + 1]) % mod;
else f[i][j][k] = (f[i][j][k] + f[i - 1][j - 1][k + 1]) % mod;
}
}
}
}
cout << f[m][n][0] << endl;
}
}
边栏推荐
猜你喜欢
随机推荐
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
软件质量评估的通用模型
tiup update
标识符、关键字、常量 和变量(C语言)
E - Distance Sequence (前缀和优化dp
TinyMCE禁用转义
软件测试面试题:手工测试与自动测试有哪些区别?
在线中文姓名生成工具推荐
Handwritten Distributed Configuration Center (1)
"No title"
【LeetCode】Summary of Two Pointer Problems
Flask框架 根据源码分析可扩展点
"Relish Podcast" #397 The factory manager is here: How to use technology to empower the law?
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
leetcode:267. 回文排列 II
E - Many Operations (bitwise consideration + dp thought to record the result after the operation
Mysql_14 存储引擎
tiup telemetry
软件测试面试题:一套完整的测试应该由哪些阶段组成?
oracle创建用户以后的权限问题









