【HDU 5184】 Brackets (卡特兰数)

Brackets



Problem Description
We give the following inductive definition of a “regular brackets” sequence:
● the empty sequence is a regular brackets sequence,
● if s is a regular brackets sequence, then (s) are regular brackets sequences, and
● if a and b are regular brackets sequences, then ab is a regular brackets sequence.
● no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:
(), (()), ()(), ()(())
while the following character sequences are not:
(, ), )(, ((), ((()

Now we want to construct a regular brackets sequence of length  n, how many regular brackets sequences we can get when the front several brackets are given already.

 

Input
Multi test cases (about  2000), every case occupies two lines.
The first line contains an integer n.
Then second line contains a string str which indicates the front several brackets.

Please process to the end of file.

[Technical Specification]
1≤n≤1000000
str contains only '(' and ')' and length of str is larger than 0 and no more than n.

 

Output
For each case,output answer %  1000000007 in a single line.

 

Sample Input
4
()
4
(
6
()
 
 
【题意】
  括号匹配(<=10^6),给出已将匹配好的前若干个的括号,让你把剩下的匹配完,问方案数%10^9+7,多组数据<=1000
 
【分析】
  跟上一题类似。先判断他给的括号是否匹配。然后假设左括号比右括号多了l个,就是要求任意前缀和>=l的方案数。也用1与-1互换的方法推
 
  
  上一个厉害的证明:
  
 
  我的理解在上一篇博写了~
 
代码如下:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cmath>
 8 using namespace std;
 9 #define Maxn 1000010
10 #define Mod 1000000007
11 #define LL long long
12 
13 char s[Maxn];
14 int p[Maxn];
15 
16 void init()
17 {
18     p[0]=1;
19     for(int i=1;i<=Maxn-10;i++)
20     {
21         LL x=(LL)p[i-1],y=(LL)i,z;
22         z=(x*y)%Mod;
23         p[i]=(int)z;
24     }
25 }
26 
27 LL qpow(int x,int b)
28 {
29     if(x==0) return 1;
30     LL xx=x,ans=1;
31     while(b)
32     {
33         if(b&1) ans=(ans*xx)%Mod;
34         xx=(xx*xx)%Mod;
35         b>>=1;
36     }
37     return ans;
38 }
39 
40 int get_c(int n,int m)
41 {
42     LL ans=p[m];
43     ans=(ans*qpow(p[n],Mod-2))%Mod;
44     ans=(ans*qpow(p[m-n],Mod-2))%Mod;
45     return (int)ans;
46 }
47 
48 int main()
49 {
50     init();
51     int n;
52     while(scanf("%d",&n)!=EOF)
53     {
54         int m,sl=0;
55         scanf("%s",s+1);
56         int l=strlen(s+1),now=0;
57         bool ok=1;
58         for(int i=1;i<=l;i++)
59         {
60             if(s[i]=='(') now++,sl++;
61             else now--;
62             if(now<0) ok=0;
63         }
64         if(n%2!=0||l>n||!ok||sl*2>n||(l-sl)*2>n) {printf("0\n");continue;}
65         m=n/2-sl;
66         if(sl==n/2||l==n) {printf("1\n");continue;}
67         printf("%d\n",(get_c(m,2*m+now)+Mod-get_c(m-1,2*m+now))%Mod);
68     }
69     return 0;
70 }
[HDU 5184]

 

2016-09-20 19:53:38

转载于:https://www.cnblogs.com/Konjakmoyu/p/5890149.html

ARM6818嵌入式视频播放器:硬解加速与Framebuffer渲染实战 嵌入式Linux音视频系统是智能座舱、工业HMI等场景的核心能力,其本质是软硬协同的实时多媒体处理系统。原理上需打通内核驱动(如VPU硬件编解码、FIMD多图层合成)、用户空间框架(GStreamer pipeline定制、SDL2硬件渲染)与应用层同步机制(PTS/HTS双时钟校准)。技术价值在于显著降低CPU负载(硬解可压至15%以下)、提升启动速度(<1.2s首帧)并保障音画同步精度(±3ms级)。典型应用场景包括车载信息娱乐、边缘AI终端及无X11的工控显示设备。本文基于ARM6818平台,深入VP 阅读详情

相关推荐

从零构建AXI接口的DDR3控制器:MIG IP核实战指南

本文详细介绍了如何从零构建AXI接口的DDR3控制器,重点解析MIG IP核的配置流程和优化策略。通过实战案例和仿真验证,帮助开发者掌握FPGA中DDR3控制器的AXI接口设计,提升高速数据传输效率。适用于图像处理、网络加速等高性能应用场景。

weixin_28256233的博客 453

Brackets(动态规划-区间dp)

Brackets(动态规划-区间dp) judge:poj 2955 source:Stanford Local 2004 Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 15405Accepted: 8061 Description We give the following inductive definition of a ...

画船听雨眠 326

ComfyUI破限版全中文AI绘画环境搭建与工作流实战指南

节点式工作流是AI绘画领域的核心技术架构,通过将图像生成过程拆分为可配置的模块化节点,实现了对Stable Diffusion模型的精细化控制。其技术原理基于数据流编程范式,每个节点负责特定功能处理,如文本编码、采样器配置和VAE解码等。这种设计在工程实践中的核心价值在于提供了可追溯、可复用的可视化工作流,特别适合需要参数调优和批量生产的商业场景。ComfyUI作为典型的节点式AI绘画工具,在破限版中针对中文用户优化了界面支持和显卡兼容性,显著降低了技术门槛。在实际应用中,结合ControlNet和LoRA

weixin_29038155的博客 458

Week12 作业 D - 选做题(括号匹配子序列)

一、题目描述 We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regular bracket

weixin_43988686的博客 356

MATLAB/Simulink与ThingSpeak集成:打通物联网数据与工业算法仿真的闭环

在工业算法开发与控制系统设计中,模型仿真与真实世界数据的脱节是常见的工程挑战。其核心原理在于构建一个双向数据流闭环,将虚拟的仿真模型与物理世界的传感器网络连接起来。这一技术方案的核心价值在于大幅缩短算法迭代周期,实现数据驱动的设计与验证。典型的应用场景包括数字孪生构建、预测性维护和硬件在环(HIL)测试的云端化。通过将MATLAB/Simulink这一强大的模型化设计环境,与ThingSpeak物联网云平台进行深度集成,工程师能够轻松地将实时或历史的物联网数据流直接注入仿真模型作为输入,同时将仿真结果实时回

weixin_34220179的博客 474

Rider高效调试UE引擎:无需编译源码的符号配置指南

本文提供了一份详尽的Rider高效调试UE引擎指南,核心在于无需编译源码即可配置调试符号。通过Epic启动器下载官方PDB文件,并在Rider中正确配置外部符号服务器路径,开发者能在10分钟内实现对引擎内部代码(如AActor::Tick)的源码级断点调试、变量查看和调用堆栈分析,极大提升问题排查效率。

weixin_29011239的博客 346

week12-作业-动态规划(三)

文章目录D-选做题-1题目:Input:Output:Sample Input:Sample Output:题目分析:代码:E-选做题-2题目:Input:Output:Sample Input:Sample Output:Hint:题目分析:代码: D-选做题-1 题目: We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets

chihiro的博客 658

win10系统下使用Mask_Rcnn训练自己的数据

版权所有,翻版必究。https://mp.csdn.net/console/editor/html/106094147 运行环境:WIN10,pycharm,相应的CUDA,CUDNN,tensorflow1.15.0,tensorflow-gpu-1.14.0,Anaconda3 第一步:Mask RCNN开源项目:https://github.com/matterport/Mask_RCNN 第一步:数据准备 首先安装labelme进行数据的标注。安装方法pip install labe

fightingxyz的博客 1752

【Week 12 作业】D

题目描述 We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regular brackets s

应是天仙狂醉,乱把白云揉碎 387

【GlobalMapper精品教程】027:路径剖面和和视线工具的使用

路径剖面视线工具允许您使用加载的高程数据集沿用户指定的路径获取垂直剖面。要定义生成3D路径剖面所遵循的路径,只需单击鼠标左键选择路径的点,然后石键单击以结束路径。出现一个带有剖面的对话框,允许您设置视线分析或优化端点。

智慧空间实验室 2058

OpenMVS :用 colmap制作自己的数据集 重建(用colmap计算位姿,将位姿转为MVS需要的格式)

数据集制作主要是在windows进行,比较方便。

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 3327

区间DP 找出最大匹配括号数

题意: We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regular brackets se

txzhangZz的博客 348

SAP 标准 CDS View 到底能不能用 CDS Extension View 扩展,我们在项目里应该这样判断

文章摘要:判断SAP标准CDS视图(如A_PRODUCT)能否扩展字段,不能仅凭命名或@Metadata.allowExtensions判断,需检查DDL源码中的@AbapCatalog.viewEnhancementCategory值。#PROJECTION_LIST允许扩展字段,#NONE则禁止。聚合视图需额外包含#GROUP_BY,UNION视图需#UNION。CDS View Extension用于新增字段,Metadata Extension仅用于添加注解。在ABAP Cloud环境中还需检查AP

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 103

hdu 5184 Brackets(卡特兰数)

Brackets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 655    Accepted Submission(s): 169 Problem Description We give the followi

TUT我好菜啊 810

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

HDU 5184 Brackets (卡特兰数)

题目: LINK 题意: 定义合法的括号序列如下:  ● 空序列是合法括号序列   ● 如果s是一个合法括号序列,那么(s)也是合法括号序列   ● 如果a和b是合法括号序列,那么ab也是合法括号序列   ● 没有其它情况是合法括号序列  给定已知括号序列的前一部分,问可以构造出多少合法序列。  可以提前判断出一些非法的情况,比如n为奇数,给定部分括号非法。之后求合法学列数量。  

Napoleon 1644

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

HDU - 5184 Brackets 解题报告(数论)(卡特兰数)(逆元)

目录题目描述思路分析完整代码 题目描述 题目:https://vjudge.net/problem/HDU-5184 大概意思:告诉你目标的字符串长度和开头的几个括号,求可以以这几个括号开头的合法括号字符串的数量。 思路分析 在这个字符串构建的过程中,需要时刻保持:左括号数大于右括号数。如果它没有给开头序列,那么,这就是卡特兰数的模型I(不打清楚的可以先下hdu 2067作为铺垫)。但是,如果给了...

nagisa2019的博客 2023

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

卡特兰数 相关问题 hdu 5184 Brackets

题解: 当n为奇数的时候答案是0。 先判断字符串的前面是否符合括号匹配,即对于任何前缀左括号个数>=右括号个数。 设左括号个数为a右括号个数为b, m=n/2,问题可以转化为在平面中从座标(a,b)沿网格走到(m,m) 且不跨过x=y这一条直线的方法数。数据太大,普通DP和搜索都不行的。 问题可以进一步转化为从(a-n,b-n)到(0,0)且不跨过x=y的方法数。再对称一下,转化到(0,0)到(

xiaoyu1_1的专栏 828

[HDU 5184][BestCoder #32]Brackets(卡特兰数+组合数学+乘法逆元)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5184思路这道题看上去是个搜索或者DP的题目,但是实际上题目只需要O(1)O(1)时间便能求出答案,这道题是由一道很经典的类卡特兰数的问题改编而来。原题是这样的:n+mn+m个人排队买票,并且满足,票价为50元,其中nn个人各手持一张50元钞票,mm个人各手持一张100元钞票,除此之外大家身上没有任何其他的

某沙茶的OI代码库 779

【做题笔记】卡特兰数

很抱歉,幻之第六人这个称号,我还不打算出让。 ——《黑子的篮球》黑子哲也

xhyu61的博客 1922

HDOJ 5184 Brackets 卡特兰数扩展

既求从点(0,0)只能向上或者向右并且不穿越y=x到达点(a,b)有多少总走法... 有公式: C(a+b,min(a,b))-C(a+b,min(a,b)-1)  ///  折纸法证明卡特兰数: http://blog.sina.com.cn/s/blog_6917f47301010cno.html Brackets Time Limit: 2000/1000 MS

码代码的猿猿 1088

[HDU 5184] Brackets

一、题目 点此看题

C202044zxy的博客 240

hdu 5184 Brackets && BestCoder Round #32

题解:1.卡特兰数的模型,维基百科上有证明 令1表示进栈,0表示出栈,则可转化为求一个2n位、含n个1、n个0的二进制数,满足从左往右扫描到任意一位时,经过的0数不多于1数。显然含n个1、n个0的2n位二进制数共有个,下面考虑不满足要求的数目。 考虑一个含n个1、n个0的2n位二进制数,扫描到第2m+1位上时有m+1个0和m个1(容易证明一定存在这样的情况),则后面的0-1排列中必

c337134154的专栏 787

POJ - 2955(2020.4.19训练C题)

Problem We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] ar...

rainbowower的博客 337

DP poj 2955 Brackets

题目地址链接:http://poj.org/problem?id=2955 题目: Brackets Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brack

开溜老A 588

poj2955 Brackets 最大括号匹配 区间动态规划

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11633   Accepted: 6145 Description We give the following inductive definition of a “regular brackets” sequence: ...

紫芝的博客 898

程序设计思维 week12 作业D-Brackets

题目 We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and if a and b are regular brackets seq

噗的博客 298
上一篇: fekit前端代码模块化工具
下一篇: 窗口迅速关闭的解决办法/scanf/if/for/break
weixin_30340819
博客等级 码龄11年 119粉丝 1373原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址