当前位置:网站首页>2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
2022-07-24 17:17:00 【Morgannr】



The question :
It is known that Bracket sequence a yes One The length is m Of Legal bracket sequence b Of Subsequence , seek Possible sequences b The number of .( Be careful , Subsequences can be discontinuous )
Ideas :
State means :dp[i][j][k] Express In the sequence b Before i In a ,a Before j Characters Included in b in , And Left parenthesis Than Right bracket many j Number of projects
According to the state representation , The final answer is :dp[m][n][0]
( This is also the first point that should be clear : When a sequence of parentheses is a legal sequence , To meet the Left 、 The number of right parentheses should be equal This condition , But this condition is obviously not a sufficient and necessary condition )
specific working means :( State shift )
We Each enumeration sequence b pass the civil examinations i Possible cases of characters , And its Is it in the sequence a Of lcs Sequence ( Longest common subsequence ) in , So there will be Four situations , Discuss... Separately :
Case one : Sequence
aOf thejThe characters are(, AndbOf theiCharacters andaOf thejCharacters formlcsSequence , So there aredp[i][j][k] = (dp[i][j][k] + dp[i-1][j-1][k-1]) % mod, in other wordsbThe first of the sequenceiCharacters are also(, that staybBeforei - 1Of the characters Left parenthesis Than Right bracket number manyk - 1individual , and The previous state Of The longest matching length isj - 1The second case : Sequence
aOf thejThe characters are), AndbOf theiCharacters andaOf thejCharacters formlcsSequence , So there aredp[i][j][k] = (dp[i][j][k] + dp[i-1][j-1][k+1]) % mod, in other wordsbThe first of the sequenceiCharacters are also), that staybBeforei - 1Of the characters Left parenthesis Than Right bracket number manyk + 1individual , and The previous state Of The longest matching length isj - 1The third case : Current position
(, however Not withaSequence numberjMatch parentheses in positions , Then there isdp[i][j][k] = (dp[i][j][k] + dp[i-1][j][k-1]) % mod, because The current position is(, therefore staybBeforei - 1Of the characters Left parenthesis Than Right bracket number manyk - 1individual , AndaThe number of sequence matches does not changeThe fourth situation : Current position
), however Not withaSequence numberjMatch parentheses in positions , Then there isdp[i][j][k] = (dp[i][j][k] + dp[i-1][j][k+1]) % mod, because The current position is), therefore staybBeforei - 1Of the characters Left parenthesis Than Right bracket number manyk + 1individual , AndaThe number of sequence matches does not change
One thing to pay attention to is the boundary problem , In the process of dynamic transfer, you cannot index the array with negative numbers .
Code :
#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)
{
// branch 4 In this case
if (j >= 1 && k >= 1 && a[j] == '(') {
// discharge (
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k - 1]) % mod;
}
if (j >= 1 && a[j] == ')') {
// discharge )
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j - 1][k + 1]) % mod;
}
if (k >= 1 && (j == 0 || a[j] == ')')) {
// discharge (
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k - 1]) % mod;
}
if (j == 0 || a[j] == '(') {
// discharge )
dp[i][j][k] = (dp[i][j][k] + dp[i - 1][j][k + 1]) % mod;
}
}
}
}
printf("%lld\n", dp[m][n][0]);
}
return 0;
}
边栏推荐
- AutoCAD - join merge command
- HCNP Routing&Switching之DHCP中继
- 安全:如何为行人提供更多保护
- The most powerful programmer on the earth is equipped with a "three piece set". Do you know what it is?
- Xxx.pro learning in QT
- portfwd 端口转发
- DHCP relay of HCNP Routing & Switching
- NC port forwarding
- 一个实际使用SwiftUI 4.0中ViewThatFits自适应视图的例子
- Small end format and big end format (little endian & big endian)
猜你喜欢
![[GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation](/img/5f/c790baf8f8e62fca36fdb4492c38b2.png)
[GNN report] Tencent AI Lab Xu TingYang: graph generation model and its application in molecular generation

Sword finger offer 22. the penultimate node in the linked list

通道的分离与合并

HCNP Routing&Switching之DHCP中继

Development dynamics | stonedb 2022 release milestone
![[sequential logic circuit] - counter](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[sequential logic circuit] - counter
Shardingsphere database read / write separation

portmap 端口转发

Sword finger offer 48. the longest substring without repeated characters

Small end format and big end format (little endian & big endian)
随机推荐
JS to implement a promise of promises/a+ specification
opencv自带颜色操作
JSP custom tag library -- select tag
Open source Invoicing system, 10 minutes to complete, it is recommended to collect!
Want to make sandbox games? Then you must not miss this plug-in (unity3d)
【零基础】充分理解WebGL(八)
量化框架backtrader之一文读懂Indicator指标
滚动条调整亮度和对比度
Still shocked by the explosion in the movie? Then you must not miss this explosive plug-in of unity
Pull and load more on wechat applet list rendering
Safety: how to provide more protection for pedestrians
还在用Xshell?你out了,推荐一个更现代的终端连接工具!
DBF menu driver: librarydatabaseproject
CDN(Content Delivery Network)内容分发网络从入门到与实战
Internet Download Manager配置
Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
Summary of ROS master-slave communication experience
Using unity to do simulation, I don't allow this chart plug-in, you don't know
Programming language exercises (I)
Buffer overflow vulnerability lab experiment record