当前位置:网站首页>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;
}
边栏推荐
- ZCMU--5066: 黑暗长廊
- 又一款装机神器
- SharkTeam完成Flow生态NFT市场MatrixMarket的安全审计
- 云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
- How to build internal Wikipedia
- Huawei cloud digital asset chain, "chain" connects the digital economy, infinite splendor
- It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv
- 【TiDB】txt文档导入数据库,这样做真的很高效
- C language function program example (super complete)
- MySQL修改端口号(修改mysql的端口号会有问题吗)
猜你喜欢
Lazada店铺如何产号高效补单?(测评自养号技术详解篇)
Laser slam:logo-loam --- code compilation, installation and gazebo test
程序员最大的浪漫~
JS page black and white background switch JS special effect
Explain prefabrication in unity in detail
【TiDB】txt文档导入数据库,这样做真的很高效
Random talk on GIS data (VI) - projection coordinate system
Space shooting Lesson 15: props
A 58 year old native of Anhui Province, he has become the largest IPO investor in Switzerland this year
Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules
随机推荐
融合数据库生态:利用 EventBridge 构建 CDC 应用
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
广和通&高通物联网技术开放日成功举办
mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
The average altitude is 4000 meters! We built a cloud on the roof of the world
到底为什么不建议使用SELECT * ?
Observer mode, object pool
取色器实战(Qt含源码)
Use order by to sort
Color finder actual combat (QT including source code)
C # basic 7-iterator and coroutine
mfc wpf winform(工业用mfc还是qt)
Two written interview questions about regular
Random talk on GIS data (VI) - projection coordinate system
4.1 Member的各种调用方式
dll反编译(反编译加密dll)
C # basic 4-written examination question 1
Dom4J的Bug
Unity foundation 3- data persistence
1 Introduction to command mode