【CF1015F】Bracket Substring(字符串DP)
题意:给定一个只由左右括号组成的字符串s,问长度为2*n的包含它的合法括号序列方案数,答案对1e9+7取模
1≤n≤100,1≤|s|≤200
思路:暴力预处理出s的每个前缀[0..i]后加左右括号分别能与原序列最长匹配到的位置,这一步也可以用KMP
设dp[i][j][k][l]为当前到第i位,未匹配的左括号数为j,当前最长能匹配到s的前k位,是否存在过全匹配s的方案数
有两种转移,第i+1位填左括号或者右括号
答案即为 simga dp[n<<1][0][i][1] (i=0..len(s))
学习用C++的string……
用C++的话DP的式子可以写的灵活一点,毕竟不是P,多用表达式
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- #define N 210
- #define oo 10000000
- #define MOD 1000000007
- int dp[N][N][N][],len[N][],n,m;
- string a;
- void Add(int &a,int b)
- {
- a+=b;
- if(a>=MOD) a-=MOD;
- }
- int calc(string b)
- {
- int len=b.size();
- for(int i=len;i>;i--)
- if(a.substr(,i)==b.substr(len-i,i)) return i;
- return ;
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- cin>>a;
- int m=a.size();
- if(a[]=='(') len[][]=;
- else len[][]=;
- string t;
- for(int i=;i<=m-;i++)
- {
- t+=a[i];
- t+='(';
- len[i+][]=calc(t);
- t.pop_back();
- t+=')';
- len[i+][]=calc(t);
- t.pop_back();
- }
- dp[][][][]=;
- for(int i=;i<=*n-;i++)
- for(int j=;j<=n;j++)
- for(int k=;k<=m;k++)
- for(int l=;l<=;l++)
- {
- if(j+<=n) Add(dp[i+][j+][len[k][]][l|len[k][]==m],dp[i][j][k][l]);
- if(j) Add(dp[i+][j-][len[k][]][l|len[k][]==m],dp[i][j][k][l]);
- }
- int ans=;
- for(int i=;i<=m;i++) Add(ans,dp[n<<][][i][]);
- printf("%d\n",ans);
- return ;
- }
【CF1015F】Bracket Substring(字符串DP)的更多相关文章
- CF1015F Bracket Substring (KMP+DP)
题目大意:给你一个长度为$n$的括号序列$T$,要求你构造一个长度为$2n$的括号序列$S$,保证这个括号序列在插入数字后一定是正确的,并且$T$是$S$的一个子串 还以为是什么纯粹的数学构造题,一通 ...
- Codeforces 1015F Bracket Substring AC自动机 + dp
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- 【BZOJ 2121】 (字符串DP,区间DP)
2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...
- AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)
引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度 pos[i][j]表示从i开始的j字符最早出现位 ...
- NOIP2015Day2T2子串(字符串dp)
又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...
- Codeforces 1150D(字符串dp)
反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...
- CF-163A Substring and Subsequence 字符串DP
Substring and Subsequence 题意 给出两个字符串s,t,求出有多少对s的子串和t的子序列相等. 思路 类似于最长公共子序列的dp数组. dp[i][j]表示s中以i为结尾的子串 ...
- [cf 1015f] Bracket Substring (dp+kmp)
传送门 Solution 设dp方程dp[now][pos][red][fla]表示还有now个位置,pos表示匹配到第几位,red表示左括号数-右括号数,fla表示是否已经是给定串的字串 暴力转移即 ...
随机推荐
- Missian指南三:创建一个Missian服务器(使用spring)
在使用Missian时,spring是可选的,但是作者本人强烈推荐和Spring配合使用.Spring是一个伟大的项目,并且它不会对程序在运行时的效率带来任何损耗. Missian在服务器端依赖与Mi ...
- C语言中可变参数的使用
在C语言程序编写中我们使用最多的函数一定包括printf以及很多类似的变形体.这个函数包含在C库函数中,定义为 int printf( const char* format, ...); 除了一个格式 ...
- cf982d Shark
ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...
- day05_06 continue语句、while循环
输入满3次跳出,然后留一句话 for i in range(3): username = input("Username:") password = input("Pas ...
- manjaro安装anaconda出错
出错信息: ==> Creating package "anaconda"... -> Generating .PKGINFO file... -> Gene ...
- jsp中/el表达式中将后台传来的时间戳格式化为年月日时分秒
sp中/el表达式中将后台传来的时间戳格式化为年月日时分秒1.引入相关标签库 <%@taglib prefix="c" uri="http://java.sun.c ...
- mojoportal在IE10中点击ImageButton出错的处理方法
在ie10中,如果点击了mojoportal中的imagebutton,会出现错误,在ie10之前的浏览器,及ie10的兼容模式中及谷歌浏览器中都不会出现. 日志中 错误信息如下: 2013-09-2 ...
- c# 操作access数据库image ole字段
using System; using System.Data; using System.Configuration; using System.Web; using System.Data.Ole ...
- Mysql Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table ...
- docker log 批量删除报错: find: `/var/lib/docker/containers/': 没有那个文件或目录
问题描述: 服务器上面docker log太多,打算用之前写的批量清理shell脚本清理掉,但是发现报错. find: `/var/lib/docker/containers/': 没有那个文件或目录 ...