UVA - 11468 (AC自动机+动态规划)

时间:2023-03-09 03:51:11
UVA - 11468  (AC自动机+动态规划)

  建立AC自动机,把AC自动机当做一张图,在上面跑L个节点就行了。

  参考了刘汝佳的代码,发现可能有一个潜在的Bug--如果模式串中出现了没有指定的字符,AC自动机可能会建立出错。

提供一组关于这个BUG的数据:

UVA - 11468  (AC自动机+动态规划)

这组数据我觉得答案应该是1吧,无论如何组合'a'和'b'这两个字符,也无法得到模式串"ac"和"bd"!!

AC代码

#include <stdio.h>
#include <string.h>
#include <queue>
#include <map>
using namespace std; const int maxnode = * + ;
const int segma_size = + + ;
const int maxn = + + + ;
const int maxs = + ;
double prob[maxn];
int id[], n, k;
char s[maxs][maxs]; struct Aho{
int ch[maxnode][segma_size];
int f[maxnode];
int match[maxnode];
int sz; void init() {
sz = ;
memset(ch[], , sizeof(ch[]));
} void insert(char *s) {
int u = , n = strlen(s);
// try to fix bug
for(int i = ; i < n; i++) {
int c = id[s[i]];
if(c == -) return;
} for(int i = ; i < n; i++) {
int c = id[s[i]];
if(!ch[u][c]) {
memset(ch[sz], , sizeof(ch[sz]));
match[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
match[u] = ;
} void getFail() {
f[] = ;
queue<int> q;
for(int i = ; i < n; i++) {
int u = ch[][i];
if(u) { f[u] = ; q.push(u); }
} while(!q.empty()) {
int r = q.front(); q.pop();
for(int c = ; c < n; 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; double d[maxnode][];
bool vis[maxnode][]; double solve(int u, int L) {
if(!L) return 1.0;
if(vis[u][L]) return d[u][L];
vis[u][L] = true;
double &ans = d[u][L];
ans = 0.0;
for(int c = ; c < n; c++) {
if(!ac.match[ac.ch[u][c]]) ans += prob[c] * solve(ac.ch[u][c], L-);
}
return ans;
} int main() {
int T, kase = ;
scanf("%d", &T);
while(T--) {
scanf("%d", &k);
for(int i = ; i < k; i++) {
scanf("%s", s[i]);
}
scanf("%d", &n);
char ch[];
memset(id, -, sizeof(id));
for(int i = ; i < n; i++) {
scanf("%s%lf", ch, &prob[i]);
id[ch[]] = i;
}
ac.init();
for(int i = ; i < k; i++) ac.insert(s[i]);
ac.getFail();
int L;
scanf("%d", &L);
memset(vis, , sizeof(vis));
printf("Case #%d: %.6f\n", kase++, solve(, L));
}
return ;
}