题目传送门

题意:训练指南P217

分析:没有模板串也就是在自动机上走L步,不走到val[u] == v的节点的概率

PS:边读边insert WA了,有毒啊!

#include <bits/stdc++.h>
using namespace std; const int K = 20 + 5;
const int L = 100 + 5;
const int NODE = K * K;
const int SIZE = 66;
int idx[256], n;
struct AC {
int ch[NODE][SIZE], fail[NODE], match[NODE], sz;
void clear(void) {
memset (ch[0], 0, sizeof (ch[0]));
sz = 1;
}
void insert(char *P) {
int u = 0, lenp = strlen (P);
for (int c, i=0; i<lenp; ++i) {
c = idx[P[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 build(void) {
queue<int> que; fail[0] = 0;
int u;
for (int c=0; c<SIZE; ++c) {
u = ch[0][c];
if (u) {
fail[u] = 0; que.push (u);
}
}
while (!que.empty ()) {
int r = que.front (); que.pop ();
for (int c=0; c<SIZE; ++c) {
int u = ch[r][c];
if (!u) {
ch[r][c] = ch[fail[r]][c]; continue;
}
que.push (u);
int v = fail[r];
while (v && !ch[v][c]) v = fail[v];
fail[u] = ch[v][c];
match[u] |= match[fail[u]];
}
}
}
}ac; double prob[SIZE];
bool vis[NODE][L];
double dp[NODE][L];
double DFS(int u, int len) {
if (!len) return 1.0;
if (vis[u][len]) return dp[u][len];
vis[u][len] = true;
double &ret = dp[u][len];
ret = 0;
for (int i=0; i<n; ++i) {
if (!ac.match[ac.ch[u][i]]) ret += prob[i] * DFS (ac.ch[u][i], len - 1);
}
return ret;
}
char pattern[30][30];
//char pattern[30]; int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
//char pattern[30];
ac.clear ();
int k; scanf ("%d", &k);
for (int i=1; i<=k; ++i) {
scanf ("%s", &pattern[i]);
//scanf ("%s", &pattern);
//ac.insert (pattern);
}
//ac.build ();
scanf ("%d", &n);
char str[3];
for (int i=0; i<n; ++i) {
scanf ("%s%lf", &str, &prob[i]);
idx[str[0]] = i;
}
for (int i=1; i<=k; ++i) ac.insert (pattern[i]);
ac.build ();
int len; scanf ("%d", &len);
memset (vis, false, sizeof (vis));
printf ("Case #%d: %.6lf\n", ++cas, DFS (0, len));
} return 0;
}

  

AC自动机+全概率+记忆化DP UVA 11468 Substring的更多相关文章

  1. BZOJ 1444 [JSOI2009]有趣的游戏 (AC自动机、概率与期望DP、矩阵乘法)

    诶这题洛谷居然没有??? 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1444 题解: 我见到主要有三种做法. 一是矩阵乘法.设\(d ...

  2. UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机

    /** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...

  3. UVA - 11324 The Largest Clique 强连通缩点+记忆化dp

    题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...

  4. Codeforces1107E Vasya and Binary String 记忆化dp

    Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...

  5. uva 11468 - Substring(AC自己主动机+概率)

    题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...

  6. Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  7. cf835(预处理 + 记忆化dp)

    题目链接: http://codeforces.com/contest/835/problem/D 题意: 定义 k 度回文串为左半部分和右半部分为 k - 1 度的回文串 . 给出一个字符串 s, ...

  8. cf779D(记忆化dp)

    题目链接: http://codeforces.com/problemset/problem/799/D 题意: 给出两个矩阵边长 a, b, 和 w, h, 以及一个 c 数组, 可选择 c 数组中 ...

  9. POJ 1088 滑雪(简单的记忆化dp)

    题目 又一道可以称之为dp的题目,虽然看了别人的代码,但是我的代码写的还是很挫,,,,,, //看了题解做的简单的记忆化dp #include<stdio.h> #include<a ...

随机推荐

  1. [Android Pro] Android 进程级别 和 oom_adj对应关系

    一 : 前台进程 (Active Process): oom_adj为0. 前台进程包括 : 1 : 活动 正在前台接收用户输入 2:活动.服务与广播接收器正在执行一个onReceive事件的处理函数 ...

  2. 月考(cogs 1176)

    [题目描述] 在上次的月考中Bugall同学违反了考场纪律还吃了处分,更可气的是在第二天的校会时 间学校就此事做了全校通报. 现已知在当天校会时间有总共N个同学听到了有关Bugall的处分决定.  B ...

  3. [转]DB2时间类函数

    Src URL:http://www.cnblogs.com/wanghonghu/archive/2012/05/25/2518604.html 1.db2可以通过SYSIBM.SYSDUMMY1. ...

  4. hdu 1203

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 思路:01背包问题,求一份都拿不到的概率,状态转移方程dp[j]=min(dp[j],dp[j- ...

  5. AJAX JSON类型返回

    文本样式和下拉样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  6. Linux SNMP oid

    http://www.debianadmin.com/linux-snmp-oids-for-cpumemory-and-disk-statistics.html

  7. Android bluetooth用户自定义数据

    default mac: [btif_core.c] btif_fetch_local_bdaddr() default device name: [btif_dm.c] btif_get_defau ...

  8. 无废话ExtJs 入门教程十一[下拉列表:Combobox]

    无废话ExtJs 入门教程十一[下拉列表:Combobox] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个一个下拉列表: 1.代码如下: 1 <!DOCT ...

  9. WCF学习笔记之消息交换模式

    在WCF通信中,有三种消息交换模式,OneWay(单向模式), Request/Reponse(请求回复模式), Duplex(双工通信模式)这三种通信方式.下面对这三种消息交换模式进行讲解. 1. ...

  10. Bag-of-words模型

    Bag-of-words模型是信息检索领域常用的文档表示方法.在信息检索中,BOW模型假定对于一个文档,忽略它的单词顺序和语法.句法等要素,将其仅仅看作是若干个词汇的集合,文档中每个单词的出现都是独立 ...