当前位置:网站首页>2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
2022-07-24 17:11:00 【Morgannr】



题意:
已知 括号序列 a 是 一个 长度为 m 的 合法括号序列 b 的 子序列,求 可能的序列 b 的数量。(注意,子序列是可以不连续的)
思路:
状态表示:dp[i][j][k] 表示 在序列 b 的前 i 位中,a 的前 j 个字符 包含在 b 中,且 左括号 比 右括号多 j 个的方案数
根据状态表示,最后的答案是:dp[m][n][0]
(这也是首先应该明确的点:当一串括号序列是一个合法序列的时候,要满足 左、右括号数量应当是相等的 这个条件,但这个条件显然不是充要条件)
具体做法:(状态转移)
我们 每次枚举序列 b 中第 i 个字符的可能情况,以及其 是否在序列 a 的 lcs 序列(最长公共子序列)中,所以就会有 四种情况,分别讨论一下:
第一种情况:序列
a的第j个字符是(,且b的第i个字符和a的第j个字符 组成lcs序列,那么有dp[i][j][k] = (dp[i][j][k] + dp[i-1][j-1][k-1]) % mod,也就是说b序列的第i个字符也是(,那么 在b的前i - 1个字符中左括号 比 右括号 数目 多k - 1个,而且 前一个状态 的 最长匹配长度是j - 1第二种情况:序列
a的第j个字符是),且b的第i个字符和a的第j个字符 组成lcs序列,那么有dp[i][j][k] = (dp[i][j][k] + dp[i-1][j-1][k+1]) % mod,也就是说b序列的第i个字符也是),那么 在b的前i - 1个字符中 左括号 比 右括号 数目 多k + 1个,而且 前一个状态 的 最长匹配长度是j - 1第三种情况:当前位置放
(,但是 不与a序列第j个位置的括号进行匹配,那么就有dp[i][j][k] = (dp[i][j][k] + dp[i-1][j][k-1]) % mod,因为 当前位置是(,所以 在b的前i - 1个字符中左括号 比 右括号 数目 多k - 1个,与a序列匹配数目不变第四种情况:当前位置放
),但是 不与a序列第j个位置的括号进行匹配,那么就有dp[i][j][k] = (dp[i][j][k] + dp[i-1][j][k+1]) % mod,因为 当前位置是),所以 在b的前i - 1个字符中左括号 比 右括号 数目 多k + 1个,与a序列匹配数目不变
需要注意的一点就是边界问题,在动态转移过程中不能出现用负数对数组进行索引的情况。
代码:
#include <bits/stdc++.h>
using namespace std;
//#define map unordered_map
//#define int long long
int n;
const int N = 210, mod = 1e9 + 7;
char a[N];
int dp[N][N][N];
signed main()
{
int T; cin >> T;
while (T--)
{
int n, m; scanf("%d%d", &n, &m);
scanf("%s", a + 1);
memset(dp, 0, sizeof dp);
dp[0][0][0] = 1;
for (int i = 1; i <= m; ++i)
{
for (int j = 0; j <= min(n, i); ++j)
{
for (int k = 0; k <= m; ++k)
{
//分4种情况
if (j >= 1 && k >= 1 && a[j] == '(') {
//放 (
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k - 1]) % mod;
}
if (j >= 1 && a[j] == ')') {
//放 )
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k + 1]) % mod;
}
if (k >= 1 && (j == 0 || a[j] == ')')) {
//放 (
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k - 1]) % mod;
}
if (j == 0 || a[j] == '(') {
//放 )
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k + 1]) % mod;
}
}
}
}
printf("%lld\n", dp[m][n][0]);
}
return 0;
}
边栏推荐
- SS-Paper【1】:Fully Convolutional Networks for Semantic Segmentation
- 会议OA项目进度(一)
- [matlab]: basic knowledge learning
- 小端格式和大端格式(Little-Endian&Big-Endian)
- 工信安全中心牵头搭建数据流通平台 蚂蚁集团等厂商提供技术支持
- 图像像素的逻辑操作
- QT QML virtual keyboard
- I'll teach you how to use NPs to build intranet penetration services. When you go out, you can easily connect your lightweight notebook to your home game console to play remotely
- The industrial information security center takes the lead in building a data circulation platform, and ant group and other manufacturers provide technical support
- PAT甲级——拼写正确
猜你喜欢
[redis] -1. two ways of setting up environment based on docker

Custom types: Enumeration

Meeting OA project progress (II)

Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable

Yolopose practice: one-stage human posture estimation with hands + code interpretation

CDN(Content Delivery Network)内容分发网络从入门到与实战

什么是模糊理论,基础,流程

QT graphical interface beginner project - unmanned aerial vehicle group combat simulation

Internet Download Manager Configuration

An example of using viewthatfits adaptive view in swiftui 4.0
随机推荐
Exception handling - a small case that takes you to solve NullPointerException
内核开发
ShardingSphere数据库读写分离
DBF menu driver: librarydatabaseproject
CPU comparison
Concept of IP, classification of IP, IP multiplexing technology
Pat a - correct spelling
HCNP Routing&Switching之DHCP中继
QML coding specification
AXI协议(1):AMBA总线介绍,AXI概念与背景介绍,AXI协议特点与功能
Zcmu--5083: number pairs of ly (C language)
Development dynamics | stonedb 2022 release milestone
双亲委派机制
The differences of several deletions in SQL
opencv自带颜色操作
Sword finger offer 48. the longest substring without repeated characters
IP第十三天笔记
Mysql增删改查、检索与约束(详细教学)
Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation
Open source Invoicing system, 10 minutes to complete, it is recommended to collect!