当前位置:网站首页>Link with Bracket Sequence I(状态基多维dp)
Link with Bracket Sequence I(状态基多维dp)
2022-07-28 19:15:00 【to cling】
Problem
给定一个长度为n的括号序列a,问:构成长度为m( n ≤ m n \leq m n≤m)的合法括号序列b有多少种方案。
Solution
f [ i ] [ j ] [ k ] : 表示长度为 i 的序列 b 包含 a 的前 j 个,且还有 k 个左括号还没有匹配的方案数 f[i][j][k]:表示长度为i的序列b包含a的前j个,且还有k个左括号还没有匹配的方案数 f[i][j][k]:表示长度为i的序列b包含a的前j个,且还有k个左括号还没有匹配的方案数
- a [ j ] = ′ ( ′ a[j] = ~'(' a[j]= ′(′
当 b [ i ] = ′ ( ′ f [ i − 1 ] [ j − 1 ] [ k − 1 ] → f [ i ] [ j ] [ k ] b[i] = ~'('~~~~f[i - 1][j - 1][k - 1] \to f[i][j][k] b[i]= ′(′ f[i−1][j−1][k−1]→f[i][j][k]
当 b [ i ] = ′ ) ′ f [ i − 1 ] [ j ] [ k + 1 ] → f [ i ] [ j ] [ k ] b[i] = ~')'~~~~f[i - 1][j][k + 1] \to f[i][j][k] b[i]= ′)′ f[i−1][j][k+1]→f[i][j][k] - a [ j ] = ′ ) ′ a[j] = ~')' a[j]= ′)′
当 b [ i ] = ′ ( ′ f [ i − 1 ] [ j ] [ k − 1 ] → f [ i ] [ j ] [ k ] b[i] = ~'('~~~~f[i - 1][j][k - 1] \to f[i][j][k] b[i]= ′(′ f[i−1][j][k−1]→f[i][j][k]
当 b [ i ] = ′ ) ′ f [ i − 1 ] [ j − 1 ] [ k + 1 ] → f [ i ] [ j ] [ k ] b[i] = ~')'~~~~f[i - 1][j - 1][k + 1] \to f[i][j][k] b[i]= ′)′ f[i−1][j−1][k+1]→f[i][j][k]
Code
int n, m;
ll f[202][202][202];
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
cin >> n >> m;
string s; cin >> s;
s = " " + s;
f[0][0][0] = 1;
for(int i = 1; i <= m; i++)
for(int j = 0; j <= min(i, n); j++)
for (int k = 0; k <= i; k++)
{
f[i][j][k] = 0;
if (j == 0)
{
if(k) f[i][j][k] += f[i - 1][j][k - 1];//第i位为左括号
f[i][j][k] += f[i - 1][j][k + 1];//第i位为右括号
}
else if (s[j] == '(')
{
if(j && k) f[i][j][k] += f[i - 1][j - 1][k - 1];//第i位为左括号
f[i][j][k] += f[i - 1][j][k + 1];//第i位为右括号
}
else
{
if(k) f[i][j][k] += f[i - 1][j][k - 1];//第i位为左括号
if(j) f[i][j][k] += f[i - 1][j - 1][k + 1];//第i位为右括号
}
f[i][j][k] %= mod;
}
cout << f[m][n][0] << endl;
}
return 0;
}
边栏推荐
- Cause analysis of restart of EMC cx4-120 SPB controller
- Looking at SQL optimization from the whole process of one query
- 广和通&高通物联网技术开放日成功举办
- Why on earth is it not recommended to use select *?
- ntp服务器 时间(查看服务器时间)
- Introduction to redis I: redis practical reading notes
- Cartoon JS shooting game source code
- C # basic 3-value type and reference type, packing and unpacking
- 【1331. 数组序号转换】
- The average altitude is 4000 meters! We built a cloud on the roof of the world
猜你喜欢

The 678th operation

【周周有奖】云原生编程挑战赛“边缘容器”赛道邀你来战!

作业 ce

【TiDB】txt文档导入数据库,这样做真的很高效
![[server data recovery] HP StorageWorks series storage RAID5 two disk failure offline data recovery case](/img/7c/d5643d27c2ca7a7aed4eb02fd81042.jpg)
[server data recovery] HP StorageWorks series storage RAID5 two disk failure offline data recovery case
![[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]](/img/63/91b53b0ba718537383a97df59fe573.png)
[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]
Looking at SQL optimization from the whole process of one query

数据库--explain的使用

两款吾爱破解优秀软件,批量查找文本,图像视频画质增强

Explain mesh Collider in unity
随机推荐
C # basic 7-iterator and coroutine
The average altitude is 4000 meters! We built a cloud on the roof of the world
既要便捷、安全+智能,也要颜值,萤石发布北斗星人脸锁DL30F和极光人脸视频锁Y3000FV
Interpretation of netappp SP sensors output content
Unity foundation 2 editor expansion
Unity foundation 4 common plug-ins
unity-shader-1
又一款装机神器
source insight 使用快捷键
MoCo V2:MoCo系列再升级
阿里云 MSE 支持 Go 语言流量防护
Use order by to sort
Meaning of disk status of EMC DataDomain
How to turn on or off the disk LED of EMC Vmax
全链路灰度在数据库上我们是怎么做的?
58岁安徽人,干出瑞士今年最大IPO 投资界
不懂就问,快速成为容器服务进阶玩家!
Space shooting Lesson 13: explosion effect
Fragment中使用ViewPager滑动浏览页面
Introduction to redis I: redis practical reading notes