首页 > 代码库 > BZOJ3300: [USACO2011 Feb]Best Parenthesis

BZOJ3300: [USACO2011 Feb]Best Parenthesis

3300: [USACO2011 Feb]Best Parenthesis

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 89  Solved: 42
[Submit][Status]

Description

Recently, the cows have been competing with strings of balanced 
parentheses and comparing them with each other to see who has the 
best one. 

Such strings are scored as follows (all strings are balanced): the 
string "()" has score 1; if "A" has score s(A) then "(A)" has score 
2*s(A); and if "A" and "B" have scores s(A) and s(B), respectively, 
then "AB" has score s(A)+s(B). For example, s("(())()") = 
s("(())")+s("()") = 2*s("()")+1 = 2*1+1 = 3. 

Bessie wants to beat all of her fellow cows, so she needs to calculate 
the score of some strings. Given a string of balanced parentheses 
of length N (2 <= N <= 100,000), help Bessie compute its score. 

计算“平衡字符串”的分数,“平衡字符串”是指由相同数量的‘(’和‘)’组成, 
且以‘(’开头,以‘)’结尾的字符串。 
计算规则: 
字符串“()”的得分是1. 
如果,平衡字符串“A”的得分是是S(A),那么字符串“(A)”得分是2*S(A) ; 
如果,“A”,“B” 得分分别是S(A)和S(B),那么平衡字符串“AB”得分为S(A)+S(B) 
例如:s("(())()") =s("(())")+s("()") = 2*s("()")+1 = 2*1+1 = 3.

Input

* Line 1: A single integer: N 

* Lines 2..N + 1: Line i+1 will contain 1 integer: 0 if the ith 
character of the string is ‘(‘, and 1 if the ith character of 
the string is ‘)‘ 
第1行:N,平衡字符串长度 
第2至N+1行:Linei+1 整数0或1,0代表字符‘(’,1代表‘)’

Output

* Line 1: The score of the string. Since this number can get quite 
large, output the score modulo 12345678910. 
计算字符串得分,结果对12345678910取模

Sample Input

6
0
0
1
1
0
1
INPUT DETAILS:

This corresponds to the string "(())()".

Sample Output


3

HINT

 

Source

Silver

题解:
题意不明。。。没有说清每个左括号能不能都匹配到右括号。。。导致我想了半天。。。
本来想在栈里面直接算出答案来的,结果发现我算不出来。。。
然后去写dfs,发现还是很好写的。。。
代码:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 100000+100014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 1234567891023 using namespace std;24 inline int read()25 {26     int x=0,f=1;char ch=getchar();27     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}28     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}29     return x*f;30 }31 int p[maxn],n,top,sta[maxn];32 inline ll dfs(int l,int r)33 {34     if(r==l+1)return 1;35     ll tmp=0;36     for(int i=l+1;i<r;i=p[i]+1)tmp+=2*dfs(i,p[i]),tmp%=mod;37     return tmp;38 }     39 int main()40 {41     freopen("input.txt","r",stdin);42     freopen("output.txt","w",stdout);43     n=read();44     for1(i,n)45     {46         int x=read();47         if(!x)sta[++top]=i;48         else p[sta[top--]]=i;49     }50     ll ans=0;51     for(int i=1;i<n;i=p[i]+1)ans+=dfs(i,p[i]),ans%=mod;52     printf("%lld\n",ans);53     return 0;54 }
View Code

 挖个坑,以后看看能不能想出在栈里直接搞定的方法。

BZOJ3300: [USACO2011 Feb]Best Parenthesis