当前位置:网站首页>"Weilai Cup" 2022 Niuke summer multi school training camp 2 k.[link with bracket sequence i] bracket sequence DP
"Weilai Cup" 2022 Niuke summer multi school training camp 2 k.[link with bracket sequence i] bracket sequence DP
2022-07-26 01:30:00 【HeartFireY】
K.Link with Bracket Sequence I (DP)
Topic analysis
Given length is n n n The bracket sequence of ( No guarantee of legality ), The length generated on this basis is m m m The number of schemes in the bracket sequence .
set up d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k] Indicates that the number of parentheses inserted is i i i、 The number of parentheses in the original sequence used is j j j, There are more left parentheses than right parentheses k k k When the number of programs . Then the final answer is d p [ m − n ] [ n ] [ 0 ] dp[m - n][n][0] dp[m−n][n][0]. Then consider how to design state transition :
First enumerate the number of parentheses inserted , The original parenthesis sequence and the number of left parentheses is more than that of right parentheses .
If the parentheses enumerated at present are left parentheses , And the number of original brackets used < n <n <n, You can put this bracket in the final sequence , That is to say :
d p [ i ] [ j + 1 ] [ k + 1 ] = ( d p [ i ] [ j + 1 ] [ k + 1 ] + d p [ i ] [ j ] [ k ] ) % m o d dp[i][j + 1][k + 1] = (dp[i][j + 1][k + 1] + dp[i][j][k]) \% mod dp[i][j+1][k+1]=(dp[i][j+1][k+1]+dp[i][j][k])%mod
If a right parenthesis is enumerated at this time , also k > 0 k>0 k>0, That is, the number of left parentheses is greater than the number of right parentheses , And the number of original brackets used < n <n <n, Put the right parenthesis in the final sequence :
d p [ i ] [ j + 1 ] [ k − 1 ] = ( d p [ i ] [ j + 1 ] [ k − 1 ] + d p [ i ] [ j ] [ k ] ) % m o d dp[i][j + 1][k - 1] = (dp[i][j + 1][k - 1] + dp[i][j][k]) \% mod dp[i][j+1][k−1]=(dp[i][j+1][k−1]+dp[i][j][k])%mod
If the number of parentheses used is n n n, Or the current enumeration to the right bracket , The left parenthesis can be inserted :
d p [ i + 1 ] [ j ] [ k + 1 ] = ( d p [ i + 1 ] [ j ] [ k + 1 ] + d p [ i ] [ j ] [ k ] ) % m o d dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + dp[i][j][k]) \% mod dp[i+1][j][k+1]=(dp[i+1][j][k+1]+dp[i][j][k])%mod
When k > 0 k > 0 k>0 when , If the number of parentheses in the original sequence is n n n, Or the current enumeration to the left bracket , You can insert the right parenthesis :
d p [ i + 1 ] [ j ] [ k − 1 ] = ( d p [ i + 1 ] [ j ] [ k − 1 ] + d p [ i ] [ j ] [ k ] ) % m o d dp[i + 1][j][k - 1] = (dp[i + 1][j][k - 1] + dp[i][j][k]) \% mod dp[i+1][j][k−1]=(dp[i+1][j][k−1]+dp[i][j][k])%mod
Code
#include <bits/stdc++.h>
#pragma gcc optimize(2)
#pragma g++ optimize(2)
// #define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
int dp[220][220][220];
inline void solve(){
// memset(dp, 0, sizeof(dp));
int m, n; cin >> n >> m;
string s; cin >> s;
// if(n & 1 || (m - n) & 1) { cout << 0 << endl; return; }
dp[0][0][0]=1;
for (int i = 0; i <= m - n; ++i)
for (int j = 0; j <= n; ++j)
for (int k = 0; k <= m; ++k){
if (s[j] == '(' && j < n)
dp[i][j + 1][k + 1] = (dp[i][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 > 0){
if (s[j] == ')' && j < n)
dp[i][j + 1][k - 1] = (dp[i][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][n][0] << endl;
for(int i = 0; i <= m + 2; i++)
for(int j = 0; j <= n + 2; j++)
for(int k = 0; k <= m + 2; k++) dp[i][j][k] = 0;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
- Kubernetes pod start process
- Two stage submission and three stage submission
- Browser development and use skills
- poj1521
- U++ learning notes ustruct, uenum declaration and function library simple function implementation
- Google Gson用法详解
- “蔚来杯“2022牛客暑期多校训练营2 个人题解集合
- I want to know how much the Commission is for opening an account. Is it safe to open an account on your mobile phone
- 快速创建题目文件夹
- Tutorial on principles and applications of database system (055) -- MySQL query (XVII): usage of mathematical functions
猜你喜欢

推荐⼀款超好⽤的UI⾃动化⼯具: UiAutomator2!

zeromq浅析

MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记

Prime Ring Problem
![[software development specification III] [software version naming Specification]](/img/dd/86f3323591ec688d6828d7075db59d.png)
[software development specification III] [software version naming Specification]

NIO简易示例

Detailed explanation of redis data structure, combined with books

机器学习:贝叶斯网络
![[unity] random generation of two-dimensional cave map](/img/ec/7433f6e942fc3d0b03cac37ac87e7b.png)
[unity] random generation of two-dimensional cave map

Sqli-labs Less7
随机推荐
Tutorial on principles and applications of database system (053) -- MySQL query (XV): usage of character functions
NIO简易示例
Browser development and use skills
Introduction to API testing
Dijkstra 求最短路
快速创建题目文件夹
Zombie‘s Treasure Chest(枚举)
poj1521
旅行 (拆点分层)
Qtreewidget dotted line setting
MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记
8、学习MySQL 创建数据表
Record a failure caused by a custom redis distributed lock
数据库系统原理与应用教程(054)—— MySQL 查询(十六):日期时间型函数的用法
Dot screen precautions
U++ learning notes ustruct, uenum declaration and function library simple function implementation
Kubernetes Pod启动流程
[go] III. The simplest restful API server
[unity] random generation of two-dimensional cave map
[software development specification II] prohibited item development specification