Topic Portal (internal title 82)
Input Format
A string line $ ss $, $ SS $ ensure contains only $ '(' $ and $ ')' $.
Output Format
A line integer representing the number of sub-sequences satisfy requirements result $ 10 $ ^ 7 + 9 in.
Sample
Sample input 1:
)(()()
Sample output 1:
6
Sample input 2:
()()()
Sample Output 2:
7
Sample input 3:
)))
Sample output 3:
0
Data range and tips
Sample explained:
The first set of examples, there are several sub-sequences satisfy condition (subscript string count from $ 1 $):
delete characters $ $ 1,5 positions to obtain $ (()) $
remove $ 1,2,3,4 $ character position to obtain $ () $
delete $ $ 1,2,4,5 position of the character, get $ () $
delete $ $ 1,2,5,6 character position to obtain $ () $
delete $ 1,3 , $ 4,5 character position to obtain $ () $
delete characters $ $ 1,3,5,6 position to obtain $ () $
data range:
Provided $ n-$ is $ SS $ length
data for $ 20 \% $ a, $ n leqslant 20 $ \
to $ 50 \% $ data, $ n \ leqslant 2,000 $
for $ 100 \% $ data, $ n \ leqslant 200,000 $
answer
From left to right around the demarcation point you can enumerate the brackets, but will obviously re-count, consider the inclusion and exclusion?
In fact, there is a simpler approach to directly request the left (right) side of the brackets must be selected just fine.
Then there is the $ n ^ 2 $ approach.
But if you have done this question: permutations and combinations , it is much simpler.
Time complexity: $ \ Theta (n) $.
Expectations score: $ 100 $ points.
Actual score: $ 100 $ points.
Code time
#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int n;
char ch[200001];
int l[200001],r[200001];
long long fac[200001],inv[200001];
long long ans;
long long qpow(long long x,long long y)
{
long long res=1;
while(y)
{
if(y&1)res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res;
}
void pre_work()
{
fac[0]=1;
for(int i=1;i<=200000;i++)
fac[i]=fac[i-1]*i%mod;
inv[200000]=qpow(fac[200000],mod-2);
for(int i=200000;i;i--)
inv[i-1]=inv[i]*i%mod;
}
long long C(long long x,long long y)
{
if(x<y)return 0;
return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
int main()
{
pre_work();
scanf("%s",ch+1);
n=strlen(ch+1);
for(int i=1;i<=n;i++)l[i]=l[i-1]+(ch[i]=='(');
for(int i=n;i;i--)r[i]=r[i+1]+(ch[i]==')');
for(int i=1;i<=n;i++)
if(ch[i]=='(')
ans=(ans+C(l[i-1]+r[i+1],r[i+1]-1))%mod;
printf("%lld",ans);
return 0;
}
rp++