AC自动机+全概率+记忆化DP UVA 11468 Substring
题意:训练指南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的更多相关文章
- BZOJ 1444 [JSOI2009]有趣的游戏 (AC自动机、概率与期望DP、矩阵乘法)
诶这题洛谷居然没有??? 题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1444 题解: 我见到主要有三种做法. 一是矩阵乘法.设\(d ...
- UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机
/** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...
- uva 11468 - Substring(AC自己主动机+概率)
题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...
- 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. ...
- cf835(预处理 + 记忆化dp)
题目链接: http://codeforces.com/contest/835/problem/D 题意: 定义 k 度回文串为左半部分和右半部分为 k - 1 度的回文串 . 给出一个字符串 s, ...
- cf779D(记忆化dp)
题目链接: http://codeforces.com/problemset/problem/799/D 题意: 给出两个矩阵边长 a, b, 和 w, h, 以及一个 c 数组, 可选择 c 数组中 ...
- POJ 1088 滑雪(简单的记忆化dp)
题目 又一道可以称之为dp的题目,虽然看了别人的代码,但是我的代码写的还是很挫,,,,,, //看了题解做的简单的记忆化dp #include<stdio.h> #include<a ...
随机推荐
- CABasicAnimation 按home键后台之后,再切回来动画就停止了
解决方法: 1. CABasicAnimation *thisAnimation = [CABasicAnimtaion animationWithKeyPath:@"transform.r ...
- XMPP框架下微信项目总结(5)花名册获取(好友列表)
---->概念 ---->添加花名册 ps:添加花名册,启动: 客户端发送请求到服务器获取好友列表信息,同时在项目中创建数据表,并保存好友列表到数据表中. ---->获取服务器保存好 ...
- 使用DateUtils和DateFormatUtils处理时间日期转换与SimpleDateFormat的区别
在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...
- hdu1722(gcd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1722 题意:要使一块蛋糕既能均分给a个人,又能均分给b个人,问至少需要分成几块(不需要每块都一样大小) ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)
题目链接:http://codeforces.com/contest/722/problem/D 1 #include <bits/stdc++.h> #include <iostr ...
- hdu 1290 切糕
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1290 思路: n条直线最多能将一个平面分成几个区域其递推公式即为:f(n)=f(n-1)+n:递推一下 ...
- Spring学习笔记—装配Bean
在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...
- 在python3.5下安装scrapy包
此前scrapy只支持python2.x 但是最新的1.1.0rc1已结开始支持py3了 如果电脑上安装了scrapy的依赖包,诸如lxml.OpenSSL 1.你直接下载Scrapy-1.1.0rc ...
- Jcapta
http://blog.csdn.net/shadowsick/article/details/8575471
- PHP面试题集
汗~~做了一下网络上的php题目,不知不觉做到现在.....把答案贴出来如果有问题请欢迎补充和指正 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) $a = da ...