当前位置:网站首页>[diary of supplementary questions] [2022 Niuke summer multi school 2] k-link with bracket sequence I
[diary of supplementary questions] [2022 Niuke summer multi school 2] k-link with bracket sequence I
2022-07-24 02:24:00 【cls1277】
Pro
https://ac.nowcoder.com/acm/contest/33187/K
Sol
Code references std Writing , Because I really can't write dp, The topic is also a sentence too
With f i , j , k f_{i,j,k} fi,j,k Represents a sequence b Before i Bit and sequence a Of LCS The length is k, And the number of left parentheses minus the number of right parentheses is j Number of alternatives .
The state transition equation can be easily seen in the following program , But it may not be easy to understand
Why do we have to else, Can't you put it directly in another bracket ? We go through k You can see the value range of ,if Judge whether it is left and right brackets , The whole of its judgment str Sequence and the empty part after it , So if this is not left ( Right ) In parentheses , It may have been “a The sequence is b The subsequence ” Conditions considered , namely a Sequence traversal completed , Then it is not left ( Right ) Brackets . The state transition equation based on this case , here LCS The length of is still k, Therefore, the third dimension is k, You can also get the second dimension according to the left and right brackets ( Left parenthesis - Right bracket ) Update relationship ( Add one or subtract one ).
In addition, there is another situation , namely a The sequence has not been traversed , At this time, consider that the next character is the left bracket or the right bracket , Similarly, consider adding one minus one according to the relationship between the left and right brackets , as well as LCS The change in length .
Questions about subscripts : Subscript plus is used in many places in the following code 1 The way , Because the loop is from 0 At the beginning , To prevent the subscript from crossing the border , And a closer look reveals : The first dimension is made up of i to update i+1 Value , This is in line with our routine dp; The second dimension is j to update j-1 perhaps j to update j+1, This is because there is no linear relationship between the difference between the number of left and right parentheses and the cycle of the first dimension , Therefore, the second dimension can only be updated according to whether it is the left bracket or the right bracket ; The third dimension has k to update k and k to update k+1 Two cases , As mentioned above , The two cases correspond to a The sequence has been traversed and a There are two cases when the sequence has not been traversed . As for subscript plus 1, Because it simplifies the processing of the beginning and avoids the case of negative subscripts .
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;
}
边栏推荐
- “我们为什么要做 iVX ? ” ——访 iVX CEO 孟智平 了解 iVX 企业文化
- ASP.NET CORE写一个缓存Attribute工具
- Today's code farmer girl learned about the express framework under node
- Research and analysis of the third-party dependency library Ag grid
- C -- bit operation
- Emmet syntax summary
- Reconnaître le Protocole de couche de transport - TCP / UDP
- On Domain Driven Design
- In depth understanding of the underlying framework of wechat applet (II) component system, exprser
- 【补题日记】[2022牛客暑期多校1]C-Grab the Seat
猜你喜欢

Magazine feature: the metauniverse will reshape our lives, and we need to make sure it gets better

JDBC tool class

Cinq ans de contact avec près d'une centaine de patrons, en tant que chasseur de têtes, j'a i découvert que le secret de la promotion n'est que quatre mots

因果学习开源项目:从预测到决策!

canvas-绘图(鼠标按下 绘制 抬起 结束)

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part2):图谱数据准备与导入

Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power

Network protocol details: UDP

The new red envelope cover platform can build the source code of the independent background of the sub station

Graduation design campus information publishing platform website source code
随机推荐
【补题日记】[2022牛客暑期多校1]D-Mocha and Railgun
NetApp FAS系列一个CIFS bug引起的控制器重启案例分享
图解数组和链表详细对比,性能测试
STM32安装教程和J-link烧录驱动安装教程【第二天】
Leetcode 70 climbing stairs, 199 right view of binary tree, 232 realizing queue with stack, 143 rearranging linked list
Network protocol details: UDP
网络协议详解 :UDP
[untitled]
Sharing a case of controller restart caused by a CIFS bug in NetApp Fas series
Opensmile introduction and problems encountered during installation
Today's code farmer girl learned about the express framework under node
Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection
Vue3 uses keep alive to cache pages, but every time you switch the tab to enter the page, it will still enter onmounted, resulting in no caching effect. Why?
Understand the transport layer protocol - tcp/udp
Tensorflow 2.0 deep learning tutorial
What's new in the ranking list in July? This language is invincible?
以科技传递温度,vivo守护生物多样性之美
Jina AI 联合Datawhale,发起学习项目!
5年接觸近百比特老板,身為獵頭的我,發現昇職的秘密不過4個字
Distributed resource management and task scheduling framework yarn