当前位置:网站首页>(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
2022-07-25 05:45:00 【AC__ dream】
subject :
The sample input :
3
2 2
()
2 4
)(
2 4
()Sample output :
1
1
2The question : Known parenthesis sequence a Is a length of m The legal bracket sequence of b The subsequence , Find the possible sequence b The number of .
analysis :f[i][j][k] In sequence b Before i In a , Contains the sequence a Before j Characters , And there are more left parentheses than right parentheses k Number of projects
The final answer is obviously f[m][n][0]
Update method :
We enumerate the sequence every time b pass the civil examinations i Possible cases of characters , And whether it participates in and sequence a Of lcs In sequence , So there are four situations , Let's discuss it separately .
Case one : Sequence a Of the j The characters are (, And b Of the i Characters and a Of the j Characters make up lcs Sequence , So there are f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k-1])%mod, in other words b The first of the sequence i The characters are (, So before i-1 Of the characters ( The number is more than ) A large number k-1 individual , And the longest matching length of the previous state is j-1
The second case : Sequence a Of the j The characters are ), And b Of the i Characters and a Of the j Characters make up lcs Sequence , So there are f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k+1])%mod, in other words b The first of the sequence i The characters are ), So before i-1 Of the characters ( The number is more than ) A large number k+1 individual , And the longest matching length of the previous state is j-1
The third case : Current position (, however a The first of the sequence j The characters are ), So I can't relate to a Sequence number j Match two locations , Then there is f[i][j][k]=(f[i][j][k]+f[i-1][j][k-1])%mod, Because the current location is (, So before i-1 A position in the ( The number is more than ) A large number k-1 individual , And a The number of sequence matches does not change
The fourth situation : Current position ), however a The first of the sequence j The characters are (, So I can't relate to a Sequence number j Match two locations , Then there is f[i][j][k]=(f[i][j][k]+f[i-1][j][k+1])%mod, Because the current location is ), So before i-1 A position in the ( The number is more than ) A large number k+1 individual , And a The 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 .
Here is the code :
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
const int N=203,mod=1e9+7;
typedef long long ll;
char s[N];
ll f[N][N][N];
//f[i][j][k] In sequence b Before i In a , Contains the sequence a Before j Characters , And there are more left parentheses than right parentheses k Number of projects
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int i=0;i<=m;i++)
for(int j=0;j<=n;j++)
for(int k=0;k<=m;k++)
f[i][j][k]=0;
f[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++)
{
// Current position (
if(j>=1&&s[j]=='('&&k>=1)
f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k-1])%mod;
// Current position )
else if(j>=1&&s[j]==')')
f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k+1])%mod;
// Current position (
if(k>=1&&(j==0||s[j]==')'))
f[i][j][k]=(f[i][j][k]+f[i-1][j][k-1])%mod;
// Current position )
if(j==0||s[j]=='(')
f[i][j][k]=(f[i][j][k]+f[i-1][j][k+1])%mod;
}
printf("%lld\n",f[m][n][0]);
}
return 0;
}
边栏推荐
- Why is it that all the games are pseudorandom and can't make true random?
- New discovery of ROS callback function
- [typescript manual]
- background
- Era5 dataset description
- HTB-Optimum
- AirServer 7.3.0中文版手机设备无线传送电脑屏幕工具
- Introduction to interface in SystemVerilog
- Mechanism and principle of multihead attention and masked attention
- Microservices and related component concepts
猜你喜欢

sqlilabs less-29

求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!

Adaptation dynamics | in June, sequoiadb completed mutual certification with five products

Programming hodgepodge (II)

QT qtextedit setting qscrollbar style sheet does not take effect solution

HTB-Arctic

C编程 --“最大子数组的和” 的动态规划的解法

Unity accesses chartandgraph chart plug-in

CCID released the "Lake warehouse integrated technology research report", and Jushan database was selected as a typical representative of domestic enterprises

Leetcode 204. count prime numbers (wonderful)
随机推荐
Oracle 用户A删除用户B名下的一张表后,用户B可以通过回收站恢复被删除的表吗?
idea常用10个快捷键
Summary of common attributes of flex layout
The difference between $write and $display in SystemVerilog
ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off
Vim配置Golang开发环境
(15)[驱动开发]过写拷贝
传输线理论之相速、相位等的概念
Ffmpeg notes (I) fundamentals of audio and video
Introduction to interface in SystemVerilog
Difference between NPX and NPM
Sword finger offer special shock edition day 9
What are the ways to realize web digital visualization?
HTB-Arctic
uniapp手机端uView的u-collapse组件高度init
Airserver 7.3.0 Chinese version mobile device wireless transmission computer screen tool
QT qtextedit setting qscrollbar style sheet does not take effect solution
easyrecovery免费数据恢复工具操作简单一键恢复数据
计算BDP值和wnd值
Mechanism and principle of multihead attention and masked attention