日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU 5184】 Brackets (卡特兰数)

發布時間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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]
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 () 【題意】 括號匹配(<=10^6),給出已將匹配好的前若干個的括號,讓你把剩下的匹配完,問方案數%10^9+7,多組數據<=1000 【分析】 跟上一題類似。先判斷他給的括號是否匹配。然后假設左括號比右括號多了l個,就是要求任意前綴和>=l的方案數。也用1與-1互換的方法推 上一個厲害的證明: 轉自:http://blog.csdn.net/i_fuqiang/article/details/8501141 我的理解在上一篇博寫了~ 代碼如下: 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

總結

以上是生活随笔為你收集整理的【HDU 5184】 Brackets (卡特兰数)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。