当前位置:网站首页>K. Link with Bracket Sequence I dp
K. Link with Bracket Sequence I dp
2022-07-26 05:48:00 【Strezia】
K
dp
好难
题意
已知括号序列 a a a 是一个长度为 m m m 的合法括号序列 b b b 的子序列,求可能的序列 b b b 的数量。 n , m ≤ 200 , T ≤ 100 , ∑ m ≤ 2000 n,m\leq 200,T\leq100,\sum m\leq 2000 n,m≤200,T≤100,∑m≤2000
思路
用 d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k] 表示长度为 i i i ,与子序列的 l c s lcs lcs 长度为 j j j ,左括号比右括号多了 k k k 个的序列数量。初始 d p [ 0 ] [ 0 ] [ 0 ] = 1 dp[0][0][0]=1 dp[0][0][0]=1 ,答案即为 d p [ m ] [ n ] [ 0 ] dp[m][n][0] dp[m][n][0] 。
对于每个长度 i = 1 → m i=1\to m i=1→m 枚举 i − 1 i-1 i−1 时的情况。
代码
int dp[maxn][maxn][maxn];//i 1~m, j 1~n, k 0~i
//前i位中与子序列的lcs长度为j且左括号比右括号多k个的合法方案数
void solve() {
cin >> n >> m;
cin >> s;
s = ' ' + s;
for(int i = 0; i <= m; i++) {
for(int j = 0; j <= n; j++) {
for(int k = 0; k <= m; k++) {
dp[i][j][k] = 0;
}
}
}
dp[0][0][0] = 1;
for(int i = 1; i <= m; i++) {
for(int j = 0; j <= n; j++) {
for(int k = 0; k <= i; k++) {
if(s[j+1] == '(') dp[i][j+1][k+1] = (dp[i][j+1][k+1] + dp[i-1][j][k]) % P;
else dp[i][j][k+1] = (dp[i][j][k+1] + dp[i-1][j][k]) % P;
if(k) {
if(s[j+1] == ')') dp[i][j+1][k-1] = (dp[i][j+1][k-1] + dp[i-1][j][k]) % P;
else dp[i][j][k-1] = (dp[i][j][k-1] + dp[i-1][j][k]) % P;
}
}
}
}
cout << dp[m][n][0] << endl;
}
边栏推荐
- Redis发布订阅
- 《MongoDB入门教程》第08篇 比较运算符
- High frequency electronic circuit review examination questions and answers
- [MySQL must know and know] time function number function string function condition judgment
- 芯片的翻新和造假,人被坑麻了
- The refurbishment and counterfeiting of chips have made people feel numb
- ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
- Use latex to typeset multiple-choice test paper
- 520 for what? DIY is a high-value RGB clock that girls want to watch
- 高手是怎样炼成的?
猜你喜欢

Polymer physics test question bank

Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情

DOM operation -- operation node

Yolov3 preparatory work

Redis master-slave replication

MongoDB 常用命令

高分子物理知识点

Full binary tree / true binary tree / complete binary tree~

这种项目,最好别接!

520 for what? DIY is a high-value RGB clock that girls want to watch
随机推荐
Use of feign (Part 2)
嵌入式通用学习路线整理
Redis发布订阅
leetcode-aboutString
SSH Remote Management
Lamp architecture
高频电子线路复习考试题及答案
C language explanation series -- understanding of functions (3) formal parameters, arguments, nested calls and chain access
Qt编写物联网管理平台47-通用数据库设置
Another open source artifact, worth collecting and learning!
520 for what? DIY is a high-value RGB clock that girls want to watch
Application and value of IVR in VoIP telephone system
Learn about spark project on nebulagraph
电机控制专栏文章汇总
金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
MongoDB 常用命令
高手是怎样炼成的?
Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
Lemon class automatic learning after all
Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情