3300: [usaco 128 Feb] Best parenthesistime limit: 10 sec memory limit: 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
Best one.
Such strings are scored as follows (all strings are balanced):
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.
Calculate the score of a balanced string. A balanced string is composed of the same number of '(' and,
String that starts with '(' and ends.
Calculation rules:
The score of the string "()" is 1.
If the score of the balanced string "a" is S (A), then the score of the string "(a)" is 2 * s ();
If the "a" and "B" scores are S (A) and S (B), the "AB" score of the balanced string is S (A) + S (B)
Example: 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
The string is ')'
1st rows: N, balanced String Length
Lines 2nd to n + 1: linei + 1 integer 0 or 1. 0 represents the character '(', 1 represents ')'
Output
* Line 1: The score of the string. Since this number can get quite
Large, output the score modulo 12345678910.
Calculate the string score. The result is a modulo of 12345678910.
Sample input6
0
0
1
1
0
1
Input details:
This corresponds to the string "(())()".
Sample output
3
Hint Source
Silver
Question: The question is unknown... It is not clear whether each left brace can match the right brace... So I thought for a long time... I wanted to calculate the answer directly in the stack, but I couldn't find it... Then write the DFS, and find that it is still very easy to write... Code:
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
After digging a hole, you can see if you can figure out how to solve it directly in the stack.
Bzoj3300: [usaco2011 Feb] Best parenthesis