题意:

给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S。

给出K个模板串,计算S不包含任何一个模板串的概率

dp【i】【j】表示走到AC自动机 i 这个节点 还需要走 j 步的概率。

表示不会概率DP ,看网上题解写的。

通过记忆化搜索去写。

注意一点字符有大小字母和数字

 #include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int T, n, m, L, id[], vis[][];
char buf[];
double dp[][], pro[]; int get_num(char ch) {
if (ch >= 'a' && ch <= 'z') return ch - 'a';
if (ch >= 'A' && ch <= 'Z') return ch - 'A' + ;
if (ch >= '' && ch <= '') return ch - '' + ;
} struct Aho_Corasick {
int next[][], fail[], End[];
int root, cnt; int newnode() {
for (int i = ; i < ; i++) next[cnt][i] = -;
End[cnt++] = ;
return cnt - ;
} void init() {
cnt = ;
root = newnode();
} void insert(char buf[]) {
int len = strlen(buf);
int now = root;
for (int i = ; i < len; i++) {
if (next[now][get_num(buf[i])] == -) next[now][get_num(buf[i])] = newnode();
now = next[now][get_num(buf[i])];
}
End[now] = ;
} void build() {
queue<int> Q;
fail[root] = root;
for (int i = ; i < ; i++)
if (next[root][i] == -) next[root][i] = root;
else {
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while (!Q.empty()) {
int now = Q.front();
Q.pop();
End[now] |= End[fail[now]];
for (int i = ; i < ; i++)
if (next[now][i] == -) next[now][i] = next[fail[now]][i];
else {
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
} double solve(int pos, int res) {
if (!res) return 1.0;
if (vis[pos][res]) return dp[pos][res];
vis[pos][res] = ;
double &ret = dp[pos][res];
ret = ;
for (int i = ; i < m; ++i) {
int idx = id[i];
if (!End[next[pos][idx]]) ret += pro[i] * solve(next[pos][idx], res - );
}
return ret;
} void debug() {
for (int i = ; i < cnt; i++) {
printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], End[i]);
for (int j = ; j < ; j++) printf("%2d", next[i][j]);
printf("]\n");
}
}
} ac; int main() {
// FIN;
int cas = ;
sfi(T);
while (T--) {
sfi(n);
ac.init();
for (int i = ; i < n; ++i) {
sfs(buf);
ac.insert(buf);
}
ac.build();
sfi(m);
for (int i = ; i < m; ++i) {
scanf("%s%lf", buf, &pro[i]);
id[i] = get_num(buf[]);
}
sfi(L);
mem(vis, );
printf("Case #%d: %.6f\n", cas++, ac.solve(, L));
}
return ;
}

Substring UVA - 11468 AC自动机+概率DP的更多相关文章

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

    将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...

  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. mysql笔试题大餐---1、组合查询方式及having

    mysql笔试题大餐---1.组合查询方式及having 一.总结 一句话总结: 实践:我之前的mysql真的学的太浅了,这种情况下,依据实践(做题)才是唯一能把它学好的方式 学的暂时够了,以实践而学 ...

  2. Ubuntu 没有 无线网 RTL8821ce 8111 8186

    1.将ubuntu的linux内核版本更改到4.14(其他版本不兼容这个无线网卡的驱动) 1.1 找到内核版本 #到 Ubuntu网站http://kernel.ubuntu.com/~kernel- ...

  3. hibernate基本配置优化

    前面已经说过hibernate的基本配置了,现在对之前的基本配置进行代码优化: 写一个读取配置工具 package tool; import org.hibernate.Session; import ...

  4. 机器学习技法笔记:Homework #6 AdaBoost&Kernel Ridge Regression相关习题

    原文地址:http://www.jianshu.com/p/9bf9e2add795 AdaBoost 问题描述 程序实现 # coding:utf-8 import math import nump ...

  5. 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷

    通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下:     步骤1:通过MyEclipse中的window->show View->ot ...

  6. C# Winform DotNetBar控件之StyleManager

    这个控件作用改变窗体样式 使用方法 拖拽一个styleManager到窗体 改变它的managerStyle,再把窗体的继承改成Office2007Form 最后一步窗体初始化控件前 改掉的Enabl ...

  7. 洛谷 P2652 同花顺(离散化)

    洛谷 P2652 同花顺(题面) 手动模拟了一下,其实离散化排序可以起很大作用题目要求花色相同,数字连续,那么我们要做的就是找一种花色,并提取出其中一串数字留下那些舍弃的牌换成相应花色,并和之前留下的 ...

  8. 从零起步 系统入门Python爬虫工程师✍✍✍

    从零起步 系统入门Python爬虫工程师 爬虫(又被称为网页蜘蛛,网络机器人)就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序. 原则上,只要是浏览器(客户端) ...

  9. css渐变写法 从左到右渐变三种颜色示例;

    background:linear-gradient(to right,#7f06a8,#a02bc2,#7f06a8)

  10. c# mvc 简洁大气官网

    后台管理 https://g.alicdn.com/idleFish-F2e/app-basic/item.html?itemid=580281597427&ut_sk=1.WCB2zfWM% ...