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)复杂度, ...
随机推荐
- Tomcat权威指南-读书摘要系列9
从源代码组建Tomcat 安装Apache Ant ant是make的开放源代码的替代品,而且是专门为java程序语言设计. Ant的最初用途是作为Tomcat的组建工具: 之后,Ant成为Java软 ...
- MySQL存储引擎对比
MySQL存储引擎对比 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL的存储引擎 大家应该知道MySQL的存储引擎应该是表级别的概念,因为我们无法再创建databas ...
- 函数和常用模块【day04】: 总结(十二)
- bzoj千题计划286:bzoj1226: [SDOI2009]学校食堂Dining
http://www.lydsy.com/JudgeOnline/problem.php?id=1226 关键点:一个人只能忍受 ‘紧跟’ 在他 后面的b个人比他先打到饭 dp[i][j][k] 前i ...
- .NET下获取应用程序目录的一些方法
今天在Console Application下搞了一个小功能,期间需要获取当前应用程序的根目录,试了很多方式,都不能直接获取到,没有像Server.MapPath()这类的方法来方便地使用. 下面列举 ...
- apache本地配置多域名(wampserver本地配置多域名)
我们在本地开发时,一般是在浏览器输入 http://localhost/项目文件夹名来测试网页文件,你有没有想过在本地在浏览器输入你自己设定的一个域名进入项目文件夹中去,本地配置多域名可以测试二级域名 ...
- poj1521
霍夫曼编码,建树 #include <cstdio> #include <cstring> #include <queue> using namespace std ...
- Python 单例模式讲解
Python 单例模式讲解 本节内容: classmethod用途 单例模式方法一 类__new__方法讲解 单例模式方法二 前言: 使用单例方法的好处:对于一个类,多次实例化会产生多个对象,若使用单 ...
- 页面嵌入隐藏iframe实现导出功能
<div style="display: none"> <form action="" name="exportExcel" ...
- mapreduce的组件介绍
第一部分:重要的组件 Combiner •什么是Combiner •combine函数把一个map函数产生的<key,value>对(多个key, value)合并成一个新的<key ...