当前位置:网站首页>(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;
}
边栏推荐
- Introduction to interface in SystemVerilog
- HTB-Beep
- 出于数据安全考虑,荷兰教育部要求学校暂停使用 Chrome 浏览器
- Leetcode 204. 计数质数(太妙了)
- 50:第五章:开发admin管理服务:3:开发【查询admin用户名是否已存在,接口】;(这个接口需要登录时才能调用;所以我们编写了拦截器,让其拦截请求,判断用户是否是登录状态;)
- Samsung folding screen has sent samples to apple and Google, and the annual production capacity will be expanded from 2.4 million to 10million!
- R language uses rowmedians function to calculate the row data median value of all data rows in dataframe
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- Msys2 common configuration
- Introduction summary of using unirx in unity
猜你喜欢

HTB-Devel

剑指 Offer 54. 二叉搜索树的第k大节点

Ffmpeg notes (I) fundamentals of audio and video

Leetcode 237. delete nodes in the linked list

Leetcode 237. 删除链表中的节点
![Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure](/img/78/2a4e9d49bee8ef839d9d86fc7c3c83.png)
Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure

Introduction to interface in SystemVerilog
![50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int](/img/1b/b8529b6f1d163a9e5d5dad2b78ce93.png)
50: Chapter 5: develop admin management service: 3: develop [query whether the admin user name already exists, interface]; (this interface can only be called when logging in; so we have written an int

npx和npm区别

What are the ways to realize web digital visualization?
随机推荐
(14)[驱动开发]配置环境 VS2019 + WDK10 写 xp驱动
求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!
出于数据安全考虑,荷兰教育部要求学校暂停使用 Chrome 浏览器
Talk about how redis handles requests
Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络
Detailed explanation of stepn chain game system development mode (Sports money making mode)
Why is it that all the games are pseudorandom and can't make true random?
Linear algebra (3)
LCP plug-in creates peer VLAN interface
systemverilog中function和task区别
Three schemes for finclip to realize wechat authorized login
Leetcode 0121. the best time to buy and sell stocks - simulation from back to front
AirServer 7.3.0中文版手机设备无线传送电脑屏幕工具
leetcode/ 前 n 个数字二进制中 1 的个数
C编程 --“最大子数组的和” 的动态规划的解法
HTB-Granpa
Oracle 用户A删除用户B名下的一张表后,用户B可以通过回收站恢复被删除的表吗?
新时代生产力工具——FlowUs 息流全方位评测
2021年ICPC陕西省赛热身赛 B.CODE(位运算)
Big talk · book sharing | Haas Internet of things device cloud integrated development framework