-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathEmpty String.cpp
61 lines (51 loc) · 1.25 KB
/
Empty String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 505;
const ll MOD = 1e9+7;
int N;
char S[maxN];
ll fac[maxN], inv[maxN], dp[maxN][maxN];
ll fastpow(ll x, ll b){
ll res = 1;
while(b){
if(b&1) res = (res * x) % MOD;
x = (x * x) % MOD;
b >>= 1;
}
return res;
}
ll choose(int n, int k){
if(k < 0 || n < k) return 0;
return fac[n] * inv[k] % MOD * inv[n-k] % MOD;
}
void init(){
fac[0] = inv[0] = 1;
for(int i = 1; i < maxN; i++){
fac[i] = (fac[i-1] * i) % MOD;
inv[i] = fastpow(fac[i], MOD-2);
}
for(int i = 0; i < maxN; i++)
for(int j = i; j < maxN; j++)
dp[i][j] = -1;
}
ll solve(int l, int r){
if((r-l+1)&1) return 0;
if(l > r) return 1;
if(dp[l][r] != -1) return dp[l][r];
ll cnt = 0;
for(int m = l+1; m <= r; m++){
if(S[l] == S[m]){
ll subcases = solve(l+1, m-1) * solve(m+1, r) % MOD;
ll aftercombine = subcases * choose((r-l+1)/2, (m-l+1)/2) % MOD;
cnt = (cnt + aftercombine) % MOD;
}
}
return dp[l][r] = cnt;
}
int main(){
scanf(" %s", S);
N = (int) strlen(S);
init();
printf("%lld\n", N&1 ? 0 : solve(0, N-1));
}