1 条题解
-
0
#include <bits/stdc++.h> using namespace std; const int maxn = 707; const long long mod = 1e9 + 7; char s[maxn]; long long f[maxn][maxn][3][3], ans; int n, pp[maxn]; stack<int> stk; void add(long long &a, long long b) { a = (a + b) % mod; } bool diff(int a, int b) { return a == 0 && b != 0 || a != 0 && b == 0; } void test(int i, int j) { for (int x = 0; x <= 2; x++) for (int y = 0; y <= 2; y++) if (f[i][j][x][y]) printf("\tf[%2d][%2d][%2d][%2d] = %lld\n", i, j, x, y, f[i][j][x][y]); } int main() { scanf("%s", s+1); n = strlen(s+1); for (int i = 1; i <= n; i++) { if (s[i] == '(') stk.push(i); else pp[i] = stk.top(), stk.pop(); } for (int l = 2; l <= n; l++) { for (int i = 1; i+l-1 <= n; i++) { int j = i + l - 1; if (l == 2) { if (pp[j] == i) { f[i][j][1][0] = 1; f[i][j][0][1] = 1; f[i][j][2][0] = 1; f[i][j][0][2] = 1; } } else if (pp[j] == i) { for (int c = 1; c <= 2; c++) { for (int x = 0; x <= 2; x++) { for (int y = 0; y <= 2; y++) { if (c != x) add(f[i][j][c][0], f[i+1][j-1][x][y]); if (c != y) add(f[i][j][0][c], f[i+1][j-1][x][y]); } } } } else { int k = pp[j]; if (i > k-1) continue; // [i, k-1] [k, j] // [c1 x] [y c2] // printf("cal [%d , %d]\n", i, j); for (int c1 = 0; c1 <= 2; c1++) { for (int c2 = 0; c2 <= 2; c2++) { for (int x = 0; x <= 2; x++) { for (int y = 0; y <= 2; y++) { if (diff(y, c2) && (!x && !y || x != y)) add(f[i][j][c1][c2], f[i][k-1][c1][x] * f[k][j][y][c2] % mod); } } } } } // test(i, j); } } for (int i = 0; i <= 2; i++) for (int j = 0; j <= 2; j++) add(ans, f[1][n][i][j]); printf("%lld\n", ans); return 0; }
- 1
信息
- ID
- 16
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- (无)
- 递交数
- 7
- 已通过
- 2
- 上传者