当前位置:网站首页>(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
2022-07-25 05:43:00 【AC__dream】
题目:
样例输入:
3
2 2
()
2 4
)(
2 4
()样例输出:
1
1
2题意:已知括号序列a是一个长度为m的合法括号序列b的子序列,求可能的序列b的数量。
分析:f[i][j][k]表示在序列b的前i位中,包含序列a的前j个字符,且左括号比右括号多k个的方案数
最后的答案显然是f[m][n][0]
更新方法:
我们每次枚举序列b中第i个字符的可能情况,以及其是否参与到与序列a的lcs序列中,所以就会有四种情况,我们分别讨论一下就行.
第一种情况:序列a的第j个字符是(,且b的第i个字符和a的第j个字符组成lcs序列,那么有f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k-1])%mod,也就是说b序列的第i个字符是(,那么前i-1个字符中(数目就比)数目多k-1个,而且前一个状态的最长匹配长度是j-1
第二种情况:序列a的第j个字符是),且b的第i个字符和a的第j个字符组成lcs序列,那么有f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k+1])%mod,也就是说b序列的第i个字符是),那么前i-1个字符中(数目就比)数目多k+1个,而且前一个状态的最长匹配长度是j-1
第三种情况:当前位置放(,但是a序列的第j个字符是),所以无法与a序列第j个位置进行匹配,那么就有f[i][j][k]=(f[i][j][k]+f[i-1][j][k-1])%mod,因为当前位置是(,所以前i-1个位置中(数目就比)数目多k-1个,与a序列匹配数目不变
第四种情况:当前位置放),但是a序列的第j个字符是(,所以无法与a序列第j个位置进行匹配,那么就有f[i][j][k]=(f[i][j][k]+f[i-1][j][k+1])%mod,因为当前位置是),所以前i-1个位置中(数目就比)数目多k+1个,与a序列匹配数目不变
需要注意的一点就是边界问题,在动态转移过程中不能出现用负数对数组进行索引的情况。
下面是代码:
#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]表示在序列b的前i位中,包含序列a的前j个字符,且左括号比右括号多k个的方案数
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++)
{
//当前位置放 (
if(j>=1&&s[j]=='('&&k>=1)
f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k-1])%mod;
//当前位置放 )
else if(j>=1&&s[j]==')')
f[i][j][k]=(f[i][j][k]+f[i-1][j-1][k+1])%mod;
//当前位置放 (
if(k>=1&&(j==0||s[j]==')'))
f[i][j][k]=(f[i][j][k]+f[i-1][j][k-1])%mod;
//当前位置放 )
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;
}
边栏推荐
- School day (summer vacation daily question 2)
- MATLAB作图实例:5:双轴图
- Summary of common attributes of flex layout
- HTB-Beep
- Big talk · book sharing | Haas Internet of things device cloud integrated development framework
- Array programming problem of CSDN programming challenge
- [typescript manual]
- Unity accesses chartandgraph chart plug-in
- Equal proportion of R language test group: use the prop.test function to test whether the success proportion of the two groups is equal
- Easyrecovery free data recovery tool is easy to operate and restore data with one click
猜你喜欢

The selection reference of Junzheng T41, T40 and T31 versions are all here

Microservice - remote invocation (feign component)

Leetcode 204. 计数质数(太妙了)

CSDN编程挑战赛之数组编程问题

HTB-Granpa

聊聊 Redis 是如何进行请求处理

Single sign on (one sign on, available everywhere)

HTB-Arctic
![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

sqlilabs less-29
随机推荐
Equal proportion of R language test group: use the prop.test function to test whether the success proportion of the two groups is equal
Introduction summary of using unirx in unity
School day (summer vacation daily question 2)
Productivity tool in the new era -- flowus information flow comprehensive evaluation
HTB-Beep
Programming hodgepodge (I)
G1 garbage collector
ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off
Ffmpeg notes (I) fundamentals of audio and video
Is the Huatai account opened by qiniu safe to use?
sqlilabs less-29
Programming hodgepodge (II)
CSDN编程挑战赛之数组编程问题
The u-collapse component of uniapp mobile uview is highly init
微服务 - 配置中心 - Nacos
Linear algebra (3)
obj文件格式与.mtl文件格式
R language obtains the data row where the nth maximum value of the specified data column is located in the data.table data
10、渲染基础
ERA5数据集说明