当前位置:网站首页>【2022牛客多校第二场】K-Link with Bracket Sequence I
【2022牛客多校第二场】K-Link with Bracket Sequence I
2022-07-27 18:55:00 【道长没有道观】
赛后感受
这场打下来有点坐牢,K题当时想假了,当时想的是组合数学+暴搜但是没有成功。没想到下来是DP,感觉有点亏,毕竟动归的本质还是组合数学,当时没有DP出来现在就来补题一下。顺便写个blog加深一下印象。
原题链接
题的大意:
给一个子串长度和原串长度。
再给的是一个子串(A) ,让求有多少个合法原串(B)。
解题思路:
DP思想:设前 i 个B里最多匹配到前 j 个A ,也就是求有多少在可能的B的方案数
这时候还需要注意仅用i j 标记的dp不一定合法,因此dp设计为三维 再设计一个K来判断知否合法,判断思路为有 K个左括号未被匹配:不为零,即为不合法。
状态转移过程:
现在考虑 如果在前i个B后面加一个左括号会怎么样
得到转移方程:dp(i,j,k)->dp(i+1,j,k)
再考虑,如果A的j+1是左括号就能匹配上,如果不是就不能匹配上
如果是dp(i,j,k)->dp(i+1,j+1,k+1)
如果不是dp(i,j,k)->dp(i+1,j,k+1)
如果是右括号 还需要看一下k是否不为零,即有未匹配的左括号
AC代码:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define Buff std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 205;
const int mod = 1e9 + 7;
int t, n, m;
int dp[N][N][N];
signed main()
{
Buff;
cin >> t;
while (t--)
{
cin >> n >> m;
string s;
cin >> s;
s = " " + s;
for (int i = 0; i <= m; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= i; k++) // k到i即可
{
dp[i][j][k] = 0;
}
dp[0][0][0] = 1; //初始化 空序列没有剩任何的括号
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
for (int k = 0; k <= i; k++)
{
if (!dp[i][j][k])
continue;
if (s[j + 1] == '(')
dp[i + 1][j + 1][k + 1] = (dp[i + 1][j + 1][k + 1] + dp[i][j][k]) % mod;//推出来的状态转移方程
else
dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + dp[i][j][k]) % mod;
if (k)
if (s[j + 1] == ')')
dp[i + 1][j + 1][k - 1] = (dp[i + 1][j + 1][k - 1] + dp[i][j][k]) % mod;
else
dp[i + 1][j][k - 1] = (dp[i + 1][j][k - 1] + dp[i][j][k]) % mod;
}
}
}
cout << dp[m][n][0] << endl;
}
return 0;
}
边栏推荐
- Dedecms dream weaving last article next article free controllable output link, title, thumbnail, time
- 综合设计一个OPPE主页--页面的精选配件的设计
- IOU target tracking II: viou tracker
- Worthington磷脂酶A2研究丨磷脂酰胆碱2-乙酰水解酶
- Feixin died in 2022: a good hand of China Mobile was broken, and 500million users became "zombies"
- Understand the communication mode of transmission media
- Multi person collaborative development specification
- Worthington毒液中核酸外切酶的特征及相关文献
- Instructions - Worthington reverse transcriptase, recombinant HIV testing program
- Installation and use tutorial of the latest version of Web vulnerability scanning tool appscan\awvs\xray
猜你喜欢

ECCV 2022 | 中科大&京东提出:数据高效的Transformer目标检测器

Force buckle 919. Complete binary tree inserter

Win11 user name and password backup method

Rust变量特点

JS closure knowledge

MySQL back to table, SQL optimization, four isolation levels, three logs binlog, redo log, undo log

Worthington毒液中核酸外切酶的特征及相关文献

Feixin died in 2022: a good hand of China Mobile was broken, and 500million users became "zombies"

ACM MM 2022 | 浙大提出:点云分割主动学习新SOTA

Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
随机推荐
中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒
Behavior level description and RTL level description
30分钟彻底弄懂 synchronized 锁升级过程
激光雷达中国前装大幕开启,数百万颗产能待消化
Puzzle (002) inner solid, outer solid, Hamilton
[R language] [1] beginners learn the grammar of R language and edit it with rstudio
Guava Cache 原理分析与最佳实践
综合设计一个OPPE主页--页面的精选配件的设计
The solution that the laptop can connect to WiFi but the browser cannot open the web page
The dplyr package of R language performs aggregation transformations of data packets and calculates the sum of packets of dataframe data
MySQL back to table, SQL optimization, four isolation levels, three logs binlog, redo log, undo log
Dobot magician robot arm - Introduction
Set up discuz forum and break the stolen database
工程技术开发的圈套与局限性
基于DSP 回传音通话降噪链路设计
Thinking about SLA of cloud computing
Win11系统更新KB5014668后点开始按钮没反应怎么办?
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
Implicit intent
Stick to one thing