当前位置:网站首页>【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I
【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I
2022-07-24 02:23:00 【cls1277】
Pro
https://ac.nowcoder.com/acm/contest/33187/K
Sol
代码参考了std的写法,因为的确是不会写的dp,讲题也是一句话带过了
以 f i , j , k f_{i,j,k} fi,j,k表示序列b的前i位与序列a的LCS长度为k,且左括号减右括号个数为j的方案数。
状态转移方程可以在下面程序中很容易看出来,不过可能不太好理解
为什么还要有else,直接放到另一种括号里不可以吗?我们通过k的取值范围可以看出来,if判断是否为左右括号,其判断的整个str序列以及之后的空的部分,所以如果此处不为左(右)括号时,可能是已经将“a的序列是b的子序列”条件考虑完毕,即a序列遍历完毕,则之后不为左(右)括号。基于这种情况的状态转移方程,此时LCS的长度不再发生变化仍然为k,因此更新时第三维为k,又可以根据括号的左右可以得到第二维(左括号-右括号)的更新关系(加一或减一)。
除此之外的另一种情况,即a序列未被遍历完毕,此时考虑下一个字符为左括号或右括号,同理根据左右括号关系考虑加一减一,以及LCS长度的变化。
关于下标的问题:下面代码中很多地方采用了下标加1的方式,是因为循环是从0开始的,防止下标越界,而仔细来看可以发现:第一维都是由i更新i+1的值,这符合我们常规的dp;第二维有j更新j-1或者j更新j+1,这是因为左右括号数量之差与第一维的循环并不存在线性关系,因此只能根据此处为左括号还是右括号更新第二维;第三维有k更新k和k更新k+1两种情况,上文已说,两种情况分别对应a序列已经遍历完毕和a序列尚未遍历完毕两种情况。而至于下标加1,是因为简化了处理开头和避免了负数下标的情况。
Code
//By cls1277
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define endl '\n'
const LL maxn = 205;
const LL mod = 1e9+7;
LL f[maxn][maxn][maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("data.txt","r",stdin);
#endif
LL t; cin>>t;
while(t--) {
LL n, m; cin>>n>>m;
// string str; cin>>(str+1);
char str[maxn]; cin>>(str+1);
if(m&1) {
cout<<0<<endl;
continue;
}
Ms(f, 0);
f[0][0][0] = 1;
Fo(i,0,m-1) {
Fo(j,0,i) {
//left-right
Fo(k,0,i) {
//lcs
if(j) {
if(str[k+1]==')') f[i+1][j-1][k+1]=(f[i+1][j-1][k+1]+f[i][j][k])%mod;
else f[i+1][j-1][k] = (f[i+1][j-1][k]+f[i][j][k])%mod;
}
if(str[k+1]=='(') f[i+1][j+1][k+1] = (f[i+1][j+1][k+1]+f[i][j][k])%mod;
else f[i+1][j+1][k] = (f[i+1][j+1][k]+f[i][j][k])%mod;
}
}
}
cout<<f[m][0][n]<<endl;
}
return 0;
}
边栏推荐
- 组件el-scrollbar的使用
- 输入cnpm -v出现cnpm : 无法加载文件 C:\Users\19457\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。
- wallys/WiFi6 MiniPCIe Module 2T2R2 × 2.4GHz 2x5GHz MT7915 MT7975
- How to do a good job of accompanying translation
- 浅谈元宇宙中DeFi的可能性和局限性
- regular expression
- 关于 SAP 电商云 Spartacus UI Transfer State 冗余 API 请求发送的讨论
- 程序员必备技能----断点调试(IDEA版)
- Jmeter+influxdb+grafana pressure measurement real-time monitoring platform construction
- Jar package used by jsonarray in main function provided by leetcode
猜你喜欢

The difference between.Split (",", -1) and.Split (",")

BPG笔记(三)

Qml- use listview to build a three-level treeview architecture
![[untitled]](/img/61/91a8a67d069193a9f3000a43ccdab9.png)
[untitled]

Network protocol details: TCP part1

关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答

The combination sum of C language power deduction question 39. Backtracking method and traversal method

奔走相告,行情与量化页面功能优化!股票量化分析工具QTYX-V2.4.5

College degree want to 0 basic programming after looking for a job feasible?

“我们为什么要做 iVX ? ” ——访 iVX CEO 孟智平 了解 iVX 企业文化
随机推荐
新红包封面平台可搭建分站独立后台的源码
Leetcode exercise -- two questions about the nearest common ancestor of binary trees
Enter cnpm -v and cnpm appears: the file c:\users\19457\appdata\roaming\npm\cnpm.ps1 cannot be loaded because running scripts is prohibited on this system.
5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字
J. Serval and essay (tarjan finds topological order)
Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words
Draw pictures with canvas
Use the hiflow scene connector to view the epidemic situation in the region every day
WordPress website SEO complete tutorial
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
Understand the transport layer protocol - tcp/udp
暗黑系王者,低照度图像增强技术解析
ASP.NET CORE写一个缓存Attribute工具
关于 SAP 电商云 Spartacus UI Transfer State 冗余 API 请求发送的讨论
[C language operation of linked list (initialization, establishment, length calculation, addition, deletion, and output of linked list)]
hdu-7141 Ball (bitset)
LeetCode 70爬楼梯、199二叉树的右视图、232用栈实现队列、143重排链表
“我们为什么要做 iVX ? ” ——访 iVX CEO 孟智平 了解 iVX 企业文化
Use of component El scrollbar