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 (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 506????Accepted Submission(s): 120


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]
1n1000000
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 ()

Sample Output
1 2 2
Hint
For the first case the only regular sequence is ()(). For the second case regular sequences are (()) and ()(). For the third case regular sequences are ()()() and ()(()).



/* ***********************************************
Author        :CKboss
Created Time  :2015年03月18日 星期三 20時10分21秒
File Name     :HDOJ5184.cpp
************************************************ */#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>using namespace std;typedef long long int LL;const int maxn=1001000;
const LL mod=1000000007LL;int n,len;
char str[maxn];LL inv[maxn];
LL jc[maxn],jcv[maxn];void init()
{inv[1]=1; jc[0]=1; jcv[0]=1;jc[1]=1; jcv[1]=1;for(int i=2;i<maxn;i++){inv[i]=inv[mod%i]*(mod-mod/i)%mod;jc[i]=(jc[i-1]*i)%mod;jcv[i]=(jcv[i-1]*inv[i])%mod;}
}LL COMB(LL n,LL m)
{if(m<0||m>n) return 0LL;if(m==0||m==n) return 1LL;LL ret=((jc[n]*jcv[n-m])%mod*jcv[m])%mod;return ret;
}int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);init();while(scanf("%d",&n)!=EOF){scanf("%s",str);len=strlen(str);bool flag=true;if(n%2==1) flag=false;int left=0,right=0;for(int i=0;i<len&&flag;i++){if(str[i]=='(') left++;else if(str[i]==')') right++;if(left>=right) continue;else flag=false;}if(flag==false) { puts("0"); continue; }int a=n/2-left; /// remain leftint b=n/2-right; /// remain rightif(b>a) swap(a,b);LL ans = (COMB(a+b,b)-COMB(a+b,b-1)+mod)%mod;cout<<ans<<endl;}return 0;
}


轉載于:https://www.cnblogs.com/mengfanrong/p/5262097.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/373481.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373481.shtml
英文地址,請注明出處:http://en.pswp.cn/news/373481.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

010-python基礎-數據類型-字符串操作

1、移除空白 1 username.strip() 2、分割 1 names "alex,jack,rain" 2 names_1 names.split(",") #  字符串分割之后變成列表 3 print(names_1) 4 #輸出 5 [alex, jack, rain] 3、合并列表各元素成為字符串 1 names_1 [alex, jack, rain]2 names_2…

重復次數最多的 子串_每日算法系列【LeetCode 424】替換后的最長重復字符

題目描述給你一個僅由大寫英文字母組成的字符串&#xff0c;你可以將任意位置上的字符替換成另外的字符&#xff0c;總共可最多替換 k 次。在執行上述操作后&#xff0c;找到包含重復字母的最長子串的長度。示例1輸入&#xff1a; s "ABAB", k 2 輸出&#xff1a; …

python基礎(一)簡單入門

一.第一個python程序 1.交互式編程 直接在命令行里面輸入python即可進入python交互式命令行&#xff0c;linux下一樣&#xff1a; 在 python 提示符中輸入以下文本信息&#xff0c;然后按 Enter 鍵查看運行效果&#xff1a; 2.腳本式編程 把代碼都寫到文件里面&#xff0c;然后…

VS2015 python

http://pgqlife.info/2015/05/05/VS-Python/ 配置文檔轉載于:https://www.cnblogs.com/itdef/p/5262712.html

了解Java弱引用

我最近沒來得及關注該博客&#xff0c;最重要的是&#xff0c;我沒有為與技術界的所有人保持聯系而致歉。 我最近偶然發現了Java 1.2以來提供的java.lang.ref包&#xff0c;但具有諷刺意味的是&#xff0c;幾天前我才知道它。 在瀏覽了幾篇有關各種引用類型和java doc的文章時&…

unbuntu 啟動任務腳本_Ubuntu下服務啟動腳本編寫

像Nginx、MySQL等服務一樣&#xff0c;在后臺運行自己編寫的抓取天氣信息的Python腳本。1.以管理員權限新建一個服務腳本文件sudo vim /etc/init.d/weather_service2.用下列模板修改該服務腳本文件#!/bin/bash### BEGIN INIT INFO## Provides: weather_service# Required-Start…

iOS開發工具——網絡封包分析工具Charles

作者 唐巧 發布于 2013年12月9日 | 1 討論 分享到&#xff1a;微博微信FacebookTwitter有道云筆記郵件分享稍后閱讀我的閱讀清單簡介 Charles是在Mac下常用的截取網絡封包的工具&#xff0c;在做iOS開發時&#xff0c;我們為了調試與服務器端的網絡通訊協議&#xff0c;常常需要…

Java Web托管選項流程圖

我經常被問到的一個問題是在何處以及如何托管Java Web應用程序。 可以在帶有嵌入式服務器的Eclipse中創建它很好&#xff0c;但是如何將它帶給人們呢&#xff1f; 長期以來&#xff0c;對于發燒友的程序員一直沒有答案。 只有昂貴和超大型的選擇。 事情最近變了&#xff0c;但這…

查找出系統中大于50k 且小于100k 的文件并刪除。

查找出系統中大于50k 且小于100k 的文件并刪除。 [rootxusx xxx]# ll -lhtotal 624K-rw-r--r-- 1 root root 576K Nov 30 21:39 1.txt-rw-r--r-- 1 root root 48K Nov 30 21:40 2.txt [rootxusx xxx]# find ./ -type f -size 1k -a -size -100k ./2.txt 轉載于:https://www.cnb…

vb.net mysql存儲圖片_怎么讓VB.NET 上傳圖片到SQL 數據庫只保存路徑,圖片保存到文件...

我的前臺代碼dimCoonAsSqlClient.SqlConnectiondimRsAsNewSqlClient.SqlCommandRs.ConnectionCoonRsNewSqlClient.SqlCommand("上傳圖片",Coon)Rs.CommandTypeCommandType.StoredPr...我的前臺代碼 dim Coon As SqlClient.SqlConnection dim Rs As New SqlClient.Sql…

[國嵌攻略][132][串口驅動實現]

如何開發Linux驅動程序 一般情況下都會有現成的驅動程序&#xff0c;不需要從零開始開發驅動程序。所以Linux驅動開發主要分為兩個步驟&#xff1a;1.讀得懂驅動程序&#xff1b;2.寫的了核心功能。 發送中斷處理程序 發送中斷處理函數在/drivers/serial/samsung.c的s3c24xx_se…

使用Regions ADF 11g進行Master Detail CRUD操作

你好 此示例演示了如何使用Regions在表之間創建Master Detail關系。 區域的主要目的是可重用性的概念。 使用區域和有限的任務流&#xff0c;我們可以將頁面重用到許多其他頁面中&#xff0c;以保持相同的功能并采用更簡潔的方法。 下載示例應用程序。 在此示例中&#xff0c;…

[轉] vim自定義配置 和 在ubnetu中安裝vim

Ubuntu 12.04安裝vim和配置 問題&#xff1a; ubuntu默認沒有安裝vim&#xff0c;出現&#xff1a; jygubuntu:~$ vim test.cThe program vim can be found in the following packages: * vim * vim-gnome * vim-tiny * vim-athena * vim-gtk * vim-noxTry: sudo apt-get insta…

win7 mysql php apache myadmin_windows下Apache+mysql+php+phpMyAdmin的安裝及配置 | 學步園

1、下載Apache ( httpd-2.2.25-win32-x86-no_ssl.msi )http://httpd.apache.org/download.cgi#apache24根據提示安裝到路徑(建議自定義路徑)&#xff0c;NetWork Domain和Server Name都輸入 localhost(訪問時使用的域名);2、下載mysql (mysql-5.5.34-win32.msi )http://dev.m…

(15) PHP 隨筆---LAMP Linux基本操作 對文件、目錄的操作

◇對目錄的操作&#xff1a; ◇創建目錄&#xff1a; mkdir Xmu //在當前目錄下創建一個名為Xmu的目錄 ◇創建多個級別目錄關系&#xff1a; mkdir -p newdir/newdir/newdir //在當前目錄下創建多個連續目錄&#xff0c;-p的意思是以遞歸的方式 ◇移動目錄(也可以針對…

具有NetBeans,嵌入式GlassFish,JPA和MySQL數據源的Arquillian

這是一個偶然的帖子。 我一直在研究交易CDI觀察者&#xff0c;并嘗試使用嵌入式GlassFish對它進行一些集成測試。 但是令人驚訝的是&#xff0c;這種方法不能很好地工作&#xff0c;我仍在弄清楚&#xff0c;使用普通的嵌入式GlassFish時問題出在哪里。 同時&#xff0c;我轉到…

hmcl手機版下載_最新HMCL下載地址

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓[16:49:27][AWT-EventQueue-0/ERROR]---- Hello Minecraft! Crash Report ----Version: 2.3.1Time: 2016-7-14Thread: Thread[AWT-EventQueue-0,6,main]Advice:無建議。Content:java.lang.IllegalStateException: Buffers have not…

為什么我會在2012年的新企業Java項目中使用Java EE而不是Spring

這個問題經常出現。 我的新項目也在2011年11月發布。 在這個新的Enterprise Java項目中&#xff0c;我將使用Java EE&#xff08;JEE&#xff09;代替Spring框架。 我知道&#xff1a;關于此主題的文章&#xff0c;博客和論壇討論都可以找到。 為什么還需要一個&#xff1f; 因…

jsp mysql 音樂網站_Maven+JSP+SSM+Mysql實現的音樂網站

項目簡介本系統基于MavenJSPSSMMysql實現的音樂網站。主要實現的功能有音樂播放、下載、上傳等幾個模塊。難度等級&#xff1a;中等技術棧編輯器Eclipse Version: 2020-03 (4.15.0)前端技術基礎&#xff1a;htmlcssJavaScript框架&#xff1a;JQueryBootstrap后端技術SpringSpr…

遙感影像濾波處理軟件 — timesat3.2

最近因為要做遙感影像的濾波處理&#xff0c;經過女神推薦&#xff0c;決定用Timesat&#xff0c;可是該軟件3.1版本只適合xp系統以及2011的matlab&#xff0c;后來在官網上找到了最新的3.2版本。支持64位操作系統以及2014的matlab。大家可以直接上官網&#xff08;http://www.…