Codeforces 1015F Bracket Substring AC自动机 + dp
生活随笔
收集整理的這篇文章主要介紹了
Codeforces 1015F Bracket Substring AC自动机 + dp
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Bracket Substring
這么垃圾的題怎么以前都不會(huì)寫啊, 現(xiàn)在一眼怎么就會(huì)啊。。。。
考慮dp[ i ][ j ][ k ][ op ] 表示 已經(jīng)填了 i 個(gè)空格, 末尾串匹配到 所給串的 第 j 個(gè), 已經(jīng)放了 k 個(gè)左括號(hào), 是否存在所給串的方案數(shù)。
因?yàn)椴黄ヅ涞牟皇菑念^開始的, 所以暴力求下一個(gè)或者直接ac自動(dòng)機(jī)都可以。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 200 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}struct Ac {int ch[N][26], f[N], tot, sz;int n, tar, dp[N][N][N][2];char s[N];inline int newNode() {tot++; f[tot] = 0;memset(ch[tot], 0, sizeof(ch[tot]));return tot;}void init(int _sz) {sz = _sz; tot = -1; newNode();}inline int idx(int c) {return c - '(';}void addStr(char* s) {int u = 0;for(int i = 0; s[i]; i++) {int c = idx(s[i]);if(!ch[u][c]) ch[u][c] = newNode();u = ch[u][c];}tar = u;}void build() {queue<int> que;for(int c = 0; c < sz; c++) {int v = ch[0][c];if(!v) ch[0][c] = 0;else f[v] = 0, que.push(v);}while(!que.empty()) {int u = que.front(); que.pop();for(int c = 0; c < sz; c++) {int v = ch[u][c];if(!v) ch[u][c] = ch[f[u]][c];else f[v] = ch[f[u]][c], que.push(v);}}}void solve() {init(2);scanf("%d", &n);scanf("%s", s);addStr(s);build();dp[0][0][0][0] = 1;for(int i = 0; i < 2 * n; i++) {for(int u = 0; u <= tot; u++) {for(int c = 0; c <= n; c++) {for(int p = 0; p < 2; p++) {if(!dp[i][u][c]) continue;if(c <= n) {int v = ch[u][0];add(dp[i + 1][v][c + 1][p || v == tar], dp[i][u][c][p]);}if(i - c < c) {int v = ch[u][1];add(dp[i + 1][v][c][p || v == tar], dp[i][u][c][p]);}}}}}int ans = 0;for(int u = 0; u <= tot; u++)add(ans, dp[2 * n][u][n][1]);printf("%d\n", ans);} } ac;int main() {ac.solve();return 0; }/* */?
轉(zhuǎn)載于:https://www.cnblogs.com/CJLHY/p/10791362.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Codeforces 1015F Bracket Substring AC自动机 + dp的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4.29python
- 下一篇: Python入门记录