将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点。

然后问题就变成了在Tire树上走L步且不经过禁止节点的概率。

根据全概率公式用记忆化搜索求解。

 #include <cstdio>
#include <cstring>
#include <queue>
using namespace std; const int maxnode = ;
const int sigma_size = ;
int idx[]; struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int match[maxnode];
int f[maxnode];
int sz; void init() { sz = ; memset(ch[], , sizeof(ch[])); } void insert(char* s)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++)
{
int c = idx[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()
{
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++)
{
int u = ch[][c];
if(u) { f[u] = ; q.push(u); }
}
while(!q.empty())
{
int r = q.front(); q.pop();
for(int c = ; 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 = + ;
char s[][];
double prob[sigma_size]; int vis[maxnode][maxl];
double d[maxnode][maxl]; double getProb(int u, int L)
{
if(L == ) return 1.0;
if(vis[u][L]) return d[u][L];
vis[u][L] = ;
double& ans = d[u][L];
ans = ;
for(int c = ; c < n; c++)
if(!ac.match[ac.ch[u][c]])
ans += prob[c] * getProb(ac.ch[u][c], L-);
return ans;
} int main()
{
//freopen("in.txt", "r", stdin); int T;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
int k, L;
scanf("%d", &k);
for(int i = ; i < k; i++) scanf("%s", s[i]); scanf("%d", &n);
for(int i = ; i < n; i++)
{
char s1[];
scanf("%s%lf", s1, &prob[i]);
idx[s1[]] = i;
} ac.init();
for(int i = ; i < k; i++) ac.insert(s[i]);
ac.getFail();
scanf("%d", &L);
memset(vis, , sizeof(vis));
printf("Case #%d: %.6f\n", kase, getProb(, L));
} return ;
}

代码君

UVa 11468 (AC自动机 概率DP) Substring的更多相关文章

  1. Substring UVA - 11468 AC自动机+概率DP

    题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...

  2. uva 11468 AC自动机+概率DP

    #include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...

  3. UVa 11468 Substring (AC自动机+概率DP)

    题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...

  4. UVA11468 Substring --- AC自动机 + 概率DP

    UVA11468 Substring 题目描述: 给定一些子串T1...Tn 每次随机选择一个字符(概率会给出) 构造一个长为n的串S,求T1...Tn不是S的子串的概率 直接把T1...Tn建成AC ...

  5. Uva 11468 AC自动机或运算

    AC自动机 UVa 11468 题意:给一些字符和各自出现的概率,在其中随机选择L次,形成长度为L的字符串S,给定K个模板串,求S不包含任意一个串的概率. 首先介绍改良版的AC自动机: 传统的AC自动 ...

  6. 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...

  7. 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法

    [BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT  30%的 ...

  8. bzoj1444 有趣的游戏(AC自动机+概率dp)

    题意: 给定n个长度为l的模式串,现在要用前m个大写字母生成一个随机串,每个字符有自己的出现几率,第一次出现的字符串获胜,求最终每个字符串的获胜几率. 分析: 容易想到先把所有的字符串建成一个AC自动 ...

  9. BZOJ1444[Jsoi2009]有趣的游戏——AC自动机+概率DP+矩阵乘法

    题目描述 输入 注意 是0<=P, n , l, m≤ 10. 输出 样例输入 input 1 3 2 2 1 2 1 2 AB BA AA input 2 3 4 2 1 2 1 2 AABA ...

随机推荐

  1. easyui 页签

    昨天开始搭后台框架,到晚上的时候遇到了一个现在觉得挺可笑但是当时一直很纠结很纠结的问题,这个问题刚刚解决出来,把它拿出来说说,让自己长点儿记性,希望大家不要犯我这个错误啊 在backstage.jsp ...

  2. git的安装使用和代码自动部署

    1.安装 http://www.cnblogs.com/sunada2005/archive/2013/06/06/3121098.html http://www.cnblogs.com/zhcncn ...

  3. jvm 之 国际酒店 6月25日上线内存溢出原因

    6月25日OMS,Ihotel上线成功后执行了一个批处理,SOA报警提示某一台IHOTEL机器调用OMS失败率大于阀值,登录这个机器后发现这台机器CPU使用率处于80%以上,调用OMS有的时候超过5秒 ...

  4. SQL语句AND 和 OR执行的优先级

    例句: ) FROM RT_CUSTALLOCRESULT WHERE REGDATE BETWEEN '2014-03-01' AND '2014-03-31' ) FROM RT_CUSTALLO ...

  5. [转载] poll()函数

    原地址:http://baike.baidu.com/view/2997591.htm   poll()函数:这个函数是某些Unix系统提供的用于执行与select()函数同等功能的函数,下面是这个函 ...

  6. spring_150802_resource

    接口Service: package com.spring.service; public interface DogPetService { public void queryAllDogPets( ...

  7. http://blog.csdn.net/luxiaoyu_sdc/article/details/7333024

    http://blog.csdn.net/luxiaoyu_sdc/article/details/7333024 http://blog.csdn.net/kkdelta/article/detai ...

  8. BFS+贪心 HDOJ 5335 Walk Out

    题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...

  9. interviewbit :Min Steps in Infinite GridBookmark Suggest Edit

    You are in an infinite 2D grid where you can move in any of the 8 directions : (x,y) to (x+1, y), (x ...

  10. iOS开发--3D Touch的基本使用

    1.桌面快捷菜单项 效果如图: 桌面快捷菜单 点击之后的效果如图: 点击桌面快捷菜单的效果 接下来看下具体实现:1).在-application:didFinishLaunchingWithOptio ...