forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1080 - Empty String.cpp
More file actions
56 lines (48 loc) · 1.32 KB
/
1080 - Empty String.cpp
File metadata and controls
56 lines (48 loc) · 1.32 KB
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
// Empty String
//
// Problem name: Empty String
// Problem Link: https://cses.fi/problemset/task/1080
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const ll INF = 1e18;
int add(int a, int b) {
if (a + b >= MOD)
return a + b - MOD;
if (a + b < 0)
return a + b + MOD;
return a + b;
}
int dp[505][505], c[505][505];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n = (int)s.size();
c[0][0] = 1;
for (int i = 1; i <= n; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) {
c[i][j] = add(c[i-1][j], c[i-1][j-1]);
}
}
for (int i = 0; i+1 <= n; i++)
dp[i+1][i] = 1;
for (int i = n-1; i >= 0; i--) {
for (int j = i+1; j < n; j+=2) {
for (int k = i+1; k <= j; k+=2) {
if (s[i] == s[k])
dp[i][j] = add(dp[i][j], (((1ll * dp[i+1][k-1] * dp[k+1][j]) % MOD) * c[(j-i+1)/2][(k-i+1)/2]) % MOD);
}
}
}
cout << dp[0][n-1] << '\n';
return 0;
}