AcWing 3420. 括号序列
原题链接
困难
作者:
张智豪
,
2021-04-29 23:57:58
,
所有人可见
,
阅读 830
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 5010, MOD = 1e9 + 7;
int n;
char str[N];
LL f[N][N];
LL work()
{
memset(f, 0, sizeof f);
f[0][0] = 1;
for (int i = 1; i <= n; i ++ )
if (str[i] == '(')
{
for (int j =1; j <= n; j ++ )
f[i][j] = f[i - 1][j -1];
}
else
{
f[i][0] = (f[i - 1][0] + f[i - 1][1]) % MOD;
for (int j = 1; j <= n; j ++ )
f[i][j] = (f[i - 1][j + 1] + f[i][j - 1]) % MOD;
}
for (int i = 0; i <= n; i ++ )
if (f[n][i])
return f[n][i];
return - 1;
}
int main()
{
scanf("%s", str + 1);
n = strlen(str + 1);
LL l = work();
reverse(str + 1, str + n + 1);
for (int i = 1; i <= n; i ++ )
if (str[i] == '(') str[i] = ')';
else str[i] = '(';
LL r = work();
printf("%lld\n", l * r % MOD);
return 0;
}
为什么你差评这么多
敢问这位仁兄,你经历了什么?