题意:给定一个只由左右括号组成的字符串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,多用表达式

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. #define N 210
  7. #define oo 10000000
  8. #define MOD 1000000007
  9.  
  10. int dp[N][N][N][],len[N][],n,m;
  11. string a;
  12.  
  13. void Add(int &a,int b)
  14. {
  15. a+=b;
  16. if(a>=MOD) a-=MOD;
  17. }
  18.  
  19. int calc(string b)
  20. {
  21. int len=b.size();
  22. for(int i=len;i>;i--)
  23. if(a.substr(,i)==b.substr(len-i,i)) return i;
  24. return ;
  25. }
  26.  
  27. int main()
  28. {
  29. int n;
  30. scanf("%d",&n);
  31. cin>>a;
  32. int m=a.size();
  33. if(a[]=='(') len[][]=;
  34. else len[][]=;
  35. string t;
  36. for(int i=;i<=m-;i++)
  37. {
  38. t+=a[i];
  39. t+='(';
  40. len[i+][]=calc(t);
  41. t.pop_back();
  42. t+=')';
  43. len[i+][]=calc(t);
  44. t.pop_back();
  45. }
  46. dp[][][][]=;
  47. for(int i=;i<=*n-;i++)
  48. for(int j=;j<=n;j++)
  49. for(int k=;k<=m;k++)
  50. for(int l=;l<=;l++)
  51. {
  52. if(j+<=n) Add(dp[i+][j+][len[k][]][l|len[k][]==m],dp[i][j][k][l]);
  53. if(j) Add(dp[i+][j-][len[k][]][l|len[k][]==m],dp[i][j][k][l]);
  54. }
  55. int ans=;
  56. for(int i=;i<=m;i++) Add(ans,dp[n<<][][i][]);
  57. printf("%d\n",ans);
  58. return ;
  59. }

【CF1015F】Bracket Substring(字符串DP)的更多相关文章

  1. CF1015F Bracket Substring (KMP+DP)

    题目大意:给你一个长度为$n$的括号序列$T$,要求你构造一个长度为$2n$的括号序列$S$,保证这个括号序列在插入数字后一定是正确的,并且$T$是$S$的一个子串 还以为是什么纯粹的数学构造题,一通 ...

  2. Codeforces 1015F Bracket Substring AC自动机 + dp

    Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...

  3. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  4. 【BZOJ 2121】 (字符串DP,区间DP)

    2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...

  5. AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)

    引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度  pos[i][j]表示从i开始的j字符最早出现位 ...

  6. NOIP2015Day2T2子串(字符串dp)

    又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...

  7. Codeforces 1150D(字符串dp)

    反思 三维的dp压根没看出来,看题解以后思路又很直观,找几道字符串dp练练才行 序列自动机和优化一维略 /* __ __ * ____| |_____| |____ * | | * | __ | * ...

  8. CF-163A Substring and Subsequence 字符串DP

    Substring and Subsequence 题意 给出两个字符串s,t,求出有多少对s的子串和t的子序列相等. 思路 类似于最长公共子序列的dp数组. dp[i][j]表示s中以i为结尾的子串 ...

  9. [cf 1015f] Bracket Substring (dp+kmp)

    传送门 Solution 设dp方程dp[now][pos][red][fla]表示还有now个位置,pos表示匹配到第几位,red表示左括号数-右括号数,fla表示是否已经是给定串的字串 暴力转移即 ...

随机推荐

  1. Missian指南三:创建一个Missian服务器(使用spring)

    在使用Missian时,spring是可选的,但是作者本人强烈推荐和Spring配合使用.Spring是一个伟大的项目,并且它不会对程序在运行时的效率带来任何损耗. Missian在服务器端依赖与Mi ...

  2. C语言中可变参数的使用

    在C语言程序编写中我们使用最多的函数一定包括printf以及很多类似的变形体.这个函数包含在C库函数中,定义为 int printf( const char* format, ...); 除了一个格式 ...

  3. cf982d Shark

    ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...

  4. day05_06 continue语句、while循环

    输入满3次跳出,然后留一句话 for i in range(3): username = input("Username:") password = input("Pas ...

  5. manjaro安装anaconda出错

    出错信息: ==> Creating package "anaconda"...  -> Generating .PKGINFO file...  -> Gene ...

  6. jsp中/el表达式中将后台传来的时间戳格式化为年月日时分秒

    sp中/el表达式中将后台传来的时间戳格式化为年月日时分秒1.引入相关标签库 <%@taglib prefix="c" uri="http://java.sun.c ...

  7. mojoportal在IE10中点击ImageButton出错的处理方法

    在ie10中,如果点击了mojoportal中的imagebutton,会出现错误,在ie10之前的浏览器,及ie10的兼容模式中及谷歌浏览器中都不会出现. 日志中 错误信息如下: 2013-09-2 ...

  8. c# 操作access数据库image ole字段

    using System; using System.Data; using System.Configuration; using System.Web; using System.Data.Ole ...

  9. 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 ...

  10. docker log 批量删除报错: find: `/var/lib/docker/containers/': 没有那个文件或目录

    问题描述: 服务器上面docker log太多,打算用之前写的批量清理shell脚本清理掉,但是发现报错. find: `/var/lib/docker/containers/': 没有那个文件或目录 ...