当前位置:网站首页>2022多校第二场 K题 Link with Bracket Sequence I
2022多校第二场 K题 Link with Bracket Sequence I
2022-08-05 00:18:00 【Rain Sure】
题目链接
题目大意
给定我们一个长度为 n n n的括号序列,并且告诉我们它是一个长度为 m m m的合法的括号序列的子串,问我们这个长度为 m m m的括号序列有多少种情况?
题解
考虑DP,设 f i , j , k f_{i,j,k} fi,j,k 表示考虑 b b b串的前i个字符,和 a a a串的最长公共子序列长度为 j j j,左括号比右括号多 k k k,每次转移的时候枚举当前是左括号还是右括号,最后答案即为 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;
}
}
边栏推荐
猜你喜欢
随机推荐
TinyMCE disable escape
【云原生--Kubernetes】Pod控制器
性能测试如何准备测试数据
Getting started with 3D modeling for games, what modeling software can I choose?
KT148A语音芯片怎么烧录语音进入芯片里面通过串口和电脑端的工具
关于使用read table 语句
MongoDB permission verification is turned on and mongoose database configuration
翁恺C语言程序设计网课笔记合集
【unity编译器扩展之模型动画拷贝】
Cython
机器学习(公式推导与代码实现)--sklearn机器学习库
国内网站用香港服务器会被封吗?
2022牛客暑期多校训练营5(BCDFGHK)
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
Metasploit-域名上线隐藏IP
软件测试面试题:LoadRunner 分为哪三个模块?
网站最终产品页使用单一入口还是多入口?
中日颜色风格
Implementation principle of golang coroutine
SQL关联表更新







![[Cloud Native--Kubernetes] Pod Controller](/img/e1/1a8cc82223f9a9be79ebbf1211e9a4.png)

