UVa 11468 (AC自动机 概率DP) Substring
2015-04-05 15:20
375 查看
将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点。
然后问题就变成了在Tire树上走L步且不经过禁止节点的概率。
根据全概率公式用记忆化搜索求解。
代码君
然后问题就变成了在Tire树上走L步且不经过禁止节点的概率。
根据全概率公式用记忆化搜索求解。
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxnode = 500; const int sigma_size = 64; int idx[256]; struct AhoCorasickAutomata { int ch[maxnode][sigma_size]; int match[maxnode]; int f[maxnode]; int sz; void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } void insert(char* s) { int u = 0, n = strlen(s); for(int i = 0; i < n; i++) { int c = idx[s[i]]; if(!ch[u][c]) { memset(ch[sz], 0, sizeof(ch[sz])); match[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } match[u] = 1; } void getFail() { queue<int> q; f[0] = 0; for(int c = 0; c < sigma_size; c++) { int u = ch[0][c]; if(u) { f[u] = 0; q.push(u); } } while(!q.empty()) { int r = q.front(); q.pop(); for(int c = 0; c < sigma_size; c++) { int u = ch[r][c]; if(!u) { ch[r][c] = ch[f[r]][c]; continue; } q.push(u); int v = f[r]; while(v && !ch[v][c]) v = f[v]; f[u] = ch[v][c]; match[u] |= match[f[u]]; } } } }ac; int n; const int maxl = 100 + 10; char s[30][30]; double prob[sigma_size]; int vis[maxnode][maxl]; double d[maxnode][maxl]; double getProb(int u, int L) { if(L == 0) return 1.0; if(vis[u][L]) return d[u][L]; vis[u][L] = 1; double& ans = d[u][L]; ans = 0; for(int c = 0; c < n; c++) if(!ac.match[ac.ch[u][c]]) ans += prob[c] * getProb(ac.ch[u][c], L-1); return ans; } int main() { //freopen("in.txt", "r", stdin); int T; scanf("%d", &T); for(int kase = 1; kase <= T; kase++) { int k, L; scanf("%d", &k); for(int i = 0; i < k; i++) scanf("%s", s[i]); scanf("%d", &n); for(int i = 0; i < n; i++) { char s1[9]; scanf("%s%lf", s1, &prob[i]); idx[s1[0]] = i; } ac.init(); for(int i = 0; i < k; i++) ac.insert(s[i]); ac.getFail(); scanf("%d", &L); memset(vis, 0, sizeof(vis)); printf("Case #%d: %.6f\n", kase, getProb(0, L)); } return 0; }
代码君
相关文章推荐
- Uva 11468 Substring——AC自动机+概率DP
- UVA 11468 - Substring (AC自动机 概率DP)
- 【Uva11468】Substring【AC自动机】【概率DP】
- UVA - 11468 Substring (AC自动机 + 概率dp)
- UVa11468 - Substring(AC+dp+概率)
- AC自动机+全概率+记忆化DP UVA 11468 Substring
- UVA 11468-Substring(AC自动机+概率dp)
- UVA 11468 Substring(AC自动机+概率DP)
- UVA 11468 Substring(AC自动机+概率DP)
- UVa 11468 Substring (AC自动机+概率DP)
- UVA 11468 Substring(AC自动机 + dp)
- UVA11468子串 AC自动机+概率DP
- UVA - 11468 Substring ( AC自动机 + dp)
- UVA 11468(Substring-AC自动机上dp)[Template:AC自动机]
- UVA - 11468 Substring,AC自动机 + DP
- UVA 11468 Substring(AC自动机+dp)
- uva 11468 AC自动机+概率DP
- UVA 11468 Substring(AC自动机 + 记忆化搜索)
- Uva11468(AC自动机+概率dp)
- uva 11468 - Substring(AC自动机+概率)