当前位置:网站首页>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;
}
}
边栏推荐
- could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
- tiup update
- 【LeetCode】双指针题解汇总
- mysql基础
- leetcode经典例题——单词拆分
- matlab中rcosdesign函数升余弦滚降成型滤波器
- golang 协程的实现原理
- Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
- 动态上传jar包热部署
- tiup update
猜你喜欢

Modelers experience sharing: model study method

【Valentine's Day special effects】--Canvas realizes full screen love

Mysql_14 存储引擎
![Couple Holding Hands [Greedy & Abstract]](/img/7d/1cafc000dc58f1c5e2e92150be7953.png)
Couple Holding Hands [Greedy & Abstract]

翁恺C语言程序设计网课笔记合集

找不到DiscoveryClient类型的Bean

简单的顺序结构程序(C语言)
Handwritten Distributed Configuration Center (1)

KT148A语音芯片ic工作原理以及芯片的内部架构描述

【云原生--Kubernetes】调度约束
随机推荐
对写作的一些感悟
E - Many Operations (按位考虑 + dp思想记录操作后的结果
Cloud native - Kubernetes 】 【 scheduling constraints
TinyMCE disable escape
leetcode经典例题——单词拆分
typeScript - Partially apply a function
Cython
Raw and scan of gorm
Implementation principle of golang coroutine
[LeetCode] Summary of Matrix Simulation Related Topics
Handwritten Distributed Configuration Center (1)
刘润直播预告 | 顶级高手,如何创造财富
电子行业MES管理系统的主要功能与用途
leetcode:267. 回文排列 II
[CVA Valuation Training Camp] Financial Modeling Guide - Lecture 1
GO中sync包自由控制并发的方法
网站最终产品页使用单一入口还是多入口?
软件测试面试题:软件验收测试的合格通过准则?
【CVA估值训练营】财务建模指南——第一讲
软件测试面试题:负载测试、容量测试、强度测试的区别?