一段括号序列被称为平衡的,如果对于任意前缀,左括号的数目都不小于右括号。
给定一段括号序列,问有多少括号子序列是平衡的。(内容相同但位置不同的算两种。)
Input
输入一行括号序列,括号序列的长度 <= 100。
Output
输出一行表示答案模 10^9 + 7 后的结果。
Sample Input
(()())
()()
Sample Output
18
4
Hint
样例一:
()__
(__)
(____)
()__
(__)
(__)
__()
__()
()()
()()
()()
()()
((_ _))
(())
(()__)
(__())
(())
(()())
解析:dp[i]表示左括号比右括号多i的方案数,当前值为左括号时 可以代替之前出现过的任意一个左括号或者作为新的括号出现,所以dp[i]+=dp[i-1],右括号同理
#include<bits/stdc++.h>
using namespace std;
const int N = 1e2+10;
const int M = 1e3;
typedef long long LL;
const LL mod = 1e9+7;
char str[200];
LL dp[N];
int main()
{
while(~scanf("%s", str))
{
memset(dp,0,sizeof(dp));
dp[0]=1;
int l=strlen(str);
for(int i=0; i<l; i++)
{
if(str[i]=='(')
{
for(int j=l+1; j>=1; j--)
{
dp[j]+=dp[j-1];
dp[j]%=mod;
}
}
else
{
for(int j=0; j<=l+1; j++)
{
dp[j]+=dp[j+1];
dp[j]%=mod;
}
}
}
cout<<dp[0]-1<<endl;
}
return 0;
}