Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接
Codeforces Round #501 (Div. 3) F. Bracket Substring
题解
官方题解
http://codeforces.com/blog/entry/60949 ....看不懂
设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了
状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 207;
const int mod = 1e9 + 7;
char s[maxn];
int next[maxn],dp[maxn][maxn][maxn],n;
void GetNext(int l) {
next[0] = 0;
for(int i = 1; i <= l; i++) {
int p = next[i-1];
while(p > 0 && s[p] != s[i]) p = next[p-1];
next[i] = p + 1;
}
}
int main() {
scanf("%d%s",&n,s + 1);
int len = strlen(s + 1);
GetNext(len);
dp[0][0][0] = 1;
for(int i = 0;i <= 2 * n;++ i)
for(int j = 0;j <= n; ++ j) {
for(int l = 0;l < len;++ l) {
int x = l + 1;
while(x > 0 && s[x] != '(') x = next[x - 1];
dp[i + 1][j + 1][x] = (dp[i + 1][j + 1][x] + dp[i][j][l]) % mod;
x = l + 1;
while(x > 0 && s[x] != ')') x = next[x - 1];
if(j > 0)
dp[i + 1][j - 1][x] = (dp[i + 1][j - 1][x] + dp[i][j][l]) % mod;
}
dp[i + 1][j + 1][len] =(dp[i + 1][j + 1][len] + dp[i][j][len]) % mod;
if(j > 0) dp[i + 1][j - 1][len] = (dp[i][j][len] + dp[i + 1][j - 1][len]) % mod;
}
cout << dp[2 * n][0][len] << endl;
return 0;
}
Codeforces Round #501 (Div. 3) F. Bracket Substring的更多相关文章
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #501 (Div. 3)
A - Points in Segments 题意:implement #include<bits/stdc++.h> using namespace std; typedef long ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
随机推荐
- shell脚本中自定义日志记录到文件
自定义日志函数和前期变量 # adirname - return absolute dirname of given file adirname() { odir=`pwd`; cd `dirname ...
- 支付宝APP支付,提示代码 ALIN10070
ALIN10070 此代码时ALI64代码拆分后的细分代码: 代表签名验证失败等相关问题: 如果近期修改过或者续签 过签约协议,也需要更新公私钥.
- 洛谷 P3994 高速公路
https://www.luogu.org/problemnew/show/P3994 设dp[i] 表示第i个城市到根节点的最小花费 dp[i]=min{ (dis[i]-dis[j])*P[i]+ ...
- CSS规范 - 最佳实践--(来自网易)
最佳选择器写法(模块) /* 这是某个模块 */ .m-nav{}/* 模块容器 */ .m-nav li,.m-nav a{}/* 先共性 优化组合 */ .m-nav li{}/* 后个性 语义化 ...
- 【CTF WEB】服务端请求伪造
服务端请求伪造 如你所愿,这次可以读取所有的图片,但是域名必须是www开头 测试方法 POST /index.php HTTP/1.1 Host: 218.2.197.236:27375 Conten ...
- mysql学习------MySQL慢查询日志
一.什么是慢查询日志 1.当查询超过一定时间没有返回结果的时候,才会记录进慢查询日志 2.慢查询日志可以帮助dba找出执行效率缓慢的sql语句,为数据库的优化工作提供帮助 3.慢查询日志默认是不开启的 ...
- WebBrowserのIEバージョンを最新にする。
WindowsフォームでWebBrowserコントロールを配置すると.IEのバージョンが 7 と古い.レジストリをいじると.IE11の Edgeモードに変更できる(参考記事).デザイン画面でWebBr ...
- SpringMVC_HelloWorld_01
通过配置文件的方式实现一个简单的HelloWorld. 源码 一.新建项目 1.新建动态web项目 2.命名工程springmvc-01 3.勾选"Generate web.xml depl ...
- Docker手动搭建sentry错误日志系统
Sentry介绍 在开发过程中,我们通过debug来排查bug,并且使用logging来记录系统的错误.但是logging有很多不足: 必须登陆到服务器查看日志文件 需要主动去查询 输出日志方式无法把 ...
- 启动tomcat的时候爆出如下错误
The JRE_HOME environment variable is not defined correctly This environment 解决办法: https://blog.csdn. ...