对于最终加入了括号的序列,对其求中缀表达式,建树。

可以发现$n-1$个运算符DFS序递增,且若一个-上方往左走了奇数次,则它就是+,否则就是-。

所以考虑DP,设$f[i][j]$表示考虑了前$i$个运算符,且最右边那条链长度为$j$的方案数。

时间复杂度$O(n^2)$。

#include<cstdio>
#define P 1000000000
int n,i,j,x,ans,f[2][5010];char s[5];
int main(){
scanf("%d%s",&n,s);
if(s[0]=='+')return puts("0"),0;
for(f[0][1]=x=1,i=2;i<n;i++,x^=1){
for(j=i;j;j--)f[x][j]=(f[x][j+1]+f[x^1][j-1])%P;
for(scanf("%s",s),j=s[0]=='+'?1:2;j<=i;j+=2)f[x][j]=0;
}
for(i=1;i<=n;i++)ans=(ans+f[x^1][i])%P;
return printf("%d",ans),0;
}

  

BZOJ2981 : [Poi2002]括号的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. javascript匹配各种括号书写是否正确

    今天在codewars上做了一道题,如下 看上去就是验证三种括号各种嵌套是否正确书写,本来一头雾水,一种括号很容易判断, 但是三种怎么判断! 本人只是个前端菜鸟,,不会什么高深的正则之类的. 于是,在 ...

  3. 明显调用的表达式前的括号必须具有(指针)函数类型 编译器错误 C2064

    看到“明显调用的表达式前的括号必须具有(指针)函数类型”这句时我才发现我的语文水平有多烂,怎么看都看不懂,折腾了半天才知道是哪里出了问题. 举个简单的例子 class CTest { void (CT ...

  4. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  5. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  6. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. SQL 去掉某字段括号中的值

    今天在数据库匹配数据的时候,发现一些数据存在别名,导致我的数据匹配不上.在这里记录分享一下 例如: 李钟硕 (Lee Jong Suk),这里我匹配的是 “李钟硕” 示例1: SELECT rever ...

随机推荐

  1. ecgcd(解二元不定方程)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=775 关于扩展欧几里得算法还是推一遍好啦: 有方程:a*x+b*y=d=gcd(a, b) ...

  2. HBase参数配置及说明(转)

    版本:0.94-cdh4.2.1 hbase-site.xml配置 hbase.tmp.dir 本地文件系统tmp目录,一般配置成local模式的设置一下,但是最好还是需要设置一下,因为很多文件都会默 ...

  3. hdu 1249 三角形

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 part=3*s*(s-1)+2 #include<stdio.h> #includ ...

  4. 【数据库】 防止sql注入,过滤敏感关键字

    private bool FilterIllegalChar(string sWord) { var result = false; var keyWord = @"select|inser ...

  5. Pyqt QDockWidget 停靠窗体

    网上的一个关于QDockWidget 停靠窗体的教程 代码: # -*- coding: utf-8 -*- from PyQt4.QtGui import * from PyQt4.QtCore i ...

  6. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  7. 无题- Anyway,Object-C

    Json String Body see here: working-with-json-in-ios-5also see here: serialize-custom-object-to-json- ...

  8. PHPCMS 实现上一篇、下一篇

    方法一:直接调用phpcms系统的函数 <div class="info"> <span>上一篇:<a href="{$previous_p ...

  9. 第十二篇:SOUI的utilities模块为什么要用DLL编译?

    SOUI相对于DuiEngine一个重要的变化就是很多模块变成了一个单独的DLL. 然后很多情况下用户可能希望整个产品就是一个EXE,原来DuiEngine提供了LIB编译模式,此时链接LIB模式的D ...

  10. .net Session 详解

    (一) 描述当用户在 Web 应用程序中导航 ASP.NET 页时,ASP.NET 会话状态使您能够存储和检索用户的值.HTTP 是一种无状态协议.这意味着 Web 服务器会将针对页面的每个 HTTP ...