当前位置:网站首页>【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;
}
边栏推荐
- 【华为HCIE安全考什么科目?华为HCIE安全考什么知识点?】
- 行为级描述与RTL级描述
- Comprehensively design an oppe homepage -- Design of selected accessories on the page
- "Geography language" large model Wenxin Ernie geol and its application
- Some operations about Anaconda (installing software and quickly opening)
- “地理-语言”大模型文心ERNIE-GeoL及应用
- app测试定位方式
- Command line PDF Converter::: fcoder 2PDF
- 工程技术开发的圈套与局限性
- 论文赏析[AAAI18]面向序列建模的元多任务学习
猜你喜欢

Unity 安装个人免费版

中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒

How to solve the problem when the Microsoft account login of the computer keeps turning around

综合设计一个OPPE主页--页面的搜素欧珀部分的样式

MAPGIS 3D pipeline modeling awakens the pulse of urban underground pipelines

Automated testing ----- selenium (II)

ACM mm 2022 | Zhejiang University proposed: point cloud segmentation, active learning of new SOTA

PG 之 Free Space Map & Visibility Map

Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase

Zhongdi Digital: integrating innovative domestic GIS to boost the construction of real 3D China
随机推荐
Mysql 数据恢复流程 基于binlog redolog undolog
Can single mode and multi-mode of industrial switches replace each other?
Pytest失败重跑
Sre: Google operation and maintenance decryption
Worthington plasma amine oxidase (PAO) instructions
Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase
ADB ~ 隐藏或禁用状态栏和虚拟按键
How to solve the problem when the Microsoft account login of the computer keeps turning around
"Harvest" NFT: 200 yuan to buy pictures on Taobao, and 300000 yuan on the chain
Win11 widget prompts how to solve the error when loading this content?
Second uncle, why is it so hot?
Behavior level description and RTL level description
R language uses t The test function performs a t-test to verify whether the population mean is a specific value (inferring the population mean from the sample set)
Command line PDF Converter::: fcoder 2PDF
Remember that resttemplate.getforentity failed to carry headers once, resttemplate exchange
MAPGIS 3D scene rendering technology and Application
Chinese and English instructions - abfluor 488 cell apoptosis detection kit
Natapp intranet penetration tool Internet access personal projects
R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses deviance function to calculate the sum of squares of residual
建筑云渲染的应用正在扩大,越来越多的行业急需可视化服务