UVa 11468 (AC自动机 概率DP) Substring
将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的更多相关文章
- Substring UVA - 11468 AC自动机+概率DP
题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...
- uva 11468 AC自动机+概率DP
#include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...
- UVa 11468 Substring (AC自动机+概率DP)
题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...
- UVA11468 Substring --- AC自动机 + 概率DP
UVA11468 Substring 题目描述: 给定一些子串T1...Tn 每次随机选择一个字符(概率会给出) 构造一个长为n的串S,求T1...Tn不是S的子串的概率 直接把T1...Tn建成AC ...
- Uva 11468 AC自动机或运算
AC自动机 UVa 11468 题意:给一些字符和各自出现的概率,在其中随机选择L次,形成长度为L的字符串S,给定K个模板串,求S不包含任意一个串的概率. 首先介绍改良版的AC自动机: 传统的AC自动 ...
- 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 ...
- 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法
[BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT 30%的 ...
- bzoj1444 有趣的游戏(AC自动机+概率dp)
题意: 给定n个长度为l的模式串,现在要用前m个大写字母生成一个随机串,每个字符有自己的出现几率,第一次出现的字符串获胜,求最终每个字符串的获胜几率. 分析: 容易想到先把所有的字符串建成一个AC自动 ...
- 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 ...
随机推荐
- iOS7 状态栏 修改为白色字体的步骤
1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的ViewController中在ViewDidLoad方 ...
- KIP-32 Add timestamps to Kafka message
通过KIP32,Kafka的每条消息都加进了时间戳,这个KIP在0.10.0.0被加入. 说到“时间”,先贴张图,娱乐一下(如果对星球大战系列电影不熟的话,请自动略过……) 这个KIP的文档在 KIP ...
- A Product Array Puzzle
Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i ...
- 几种CPU架构
原文链接:http://blog.csdn.net/wyzxg/article/details/5027738 这几天在下载RPM包的时候,总会看见x86,x86-64,IA64,i386,i586等 ...
- https://google-developers.appspot.com/chart/
https://google-developers.appspot.com/chart/
- POJ 1458
#include <iostream> #include <string> #define MAXN 1000 using namespace std; string s_1; ...
- House Robber II
https://leetcode.com/problems/house-robber-ii/ Note: This is an extension of House Robber. After rob ...
- 传说中的WCF(6):数据协定(b)
我们继续,上一回我们了解了数据协定的一部分内容,今天我们接着来做实验.好的,实验之前先说一句:实验有风险,写代码须谨慎. 实验开始!现在,我们定义两个带数据协定的类——Student和AddrInfo ...
- 浅谈mysql中varchar(m)与char(n)的区别与联系
mysql建表长度的限制 在mysql建表时,出现以下报错信息: 错误一:行大小过大,所使用的表这种类型的最大的行大小,不算BLOB类型,是65535.(这是我翻译的) 原因是MySQL在建表的 ...
- C 和C++ 名称修饰规则
C名称修饰规则 1.对于使用__cdecl调用约定的函数,在函数名称前加一下划线,不考虑参数和返回值. 2.对于使用__fastcall调用约定的函数,在函数名称前后各加一@符号,后跟参数的长度,不考 ...