/**
链接:https://vjudge.net/problem/UVA-11468
详见lrj训练指南P218 我的是反向求存在模板串的概率。
dp[i][j]表示当前i位置选择字符,前面i-1个字符在自动机的匹配节点编号为j时候的状态可以获得的存在概率。 书上的好简洁,求idx(c)直接利用已经给定的n个字符的下标作为结果。
正向求解!厉害。
*/
#include<bits/stdc++.h>
using namespace std;
#define P pair<int,int>
#define ms(x,y) memset(x,y,sizeof x)
#define LL long long
const int maxn = ;
const int mod = 1e9+;
const int maxnode = *+;
const int sigma_size = ;
struct AhoCorasickAutomata
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
int f[maxnode];
int last[maxnode];
void clear(){sz = ; memset(ch[],,sizeof ch[]); }
int idx(char c){
if(c>='a'&&c<='z') return c-'a';
else if(c>='A'&&c<='Z') return c-'A'+;
else return c-''+;
} void insert(char *s,int v)
{
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]);
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
} int find(int j,char b){
int c = idx(b);
j = ch[j][c];
//if(val[j]) print(j);
//else if(last[j]) print(last[j]);
return j;
} void print(int j)
{
if(j){
//cnt[val[j]]++;
print(last[j]);
}
} void getFail(){
queue<int> q;
f[] = ;
for(int c = ; c < sigma_size; c++){
int u = ch[][c];
if(u){f[u] = ; q.push(u); last[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;
}//if(!u) continue;
q.push(u);
int v = f[r];
while(v&&!ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} } ac ;
char s[];
char a[];
double p[];
double dp[][*+];
const double eps = 1e-;
int L, n;
double dfs(int i,int j)///第一维表示第i个位置选择字符。j表示前面的字符串匹配后在自动机上的节点编号。
{
if(ac.val[j]||ac.last[j]) return ;///该位置在某个模板串的结尾。
if(i==L) return ;
if(dp[i][j]>-eps) return dp[i][j];
double &res = dp[i][j];
res = ;
for(int k = ; k < n; k++){
int x = ac.find(j,a[k]);
res += p[k]*dfs(i+,x);
}
return res;
}
int main()
{
int T, k;
int cas = ;
cin>>T;
while(T--)
{
scanf("%d",&k);
ac.clear();
for(int i = ; i <= k; i++){
scanf("%s",s);
ac.insert(s,);
}
ac.getFail();
scanf("%d",&n);
for(int i = ; i < n; i++){
cin>>a[i]>>p[i];
}
scanf("%d",&L);
ms(dp,-);
printf("Case #%d: %f\n",cas++,-dfs(,));
}
return ;
}

UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机的更多相关文章

  1. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  2. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  3. HUD 1501 Zipper(记忆化 or DP)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  4. HDU1978How Many Ways 记忆化dfs+dp

    /*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...

  5. HDU2222(AC自动机入门题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. UVA 11884 A Shooting Game(记忆化搜索)

    A and B are playing a shooting game on a battlefield consisting of square-shaped unit blocks. The bl ...

  7. UVA 10400 Game Show Math (dfs + 记忆化搜索)

    Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...

  8. C#LeetCode刷题-记忆化

    记忆化篇 # 题名 刷题 通过率 难度 329 矩阵中的最长递增路径   31.0% 困难

  9. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

随机推荐

  1. Linux内存使用方法详细解析

    我是一名程序员,那么我在这里以一个程序员的角度来讲解Linux内存的使用. 一提到内存管理,我们头脑中闪出的两个概念,就是虚拟内存,与物理内存.这两个概念主要来自于linux内核的支持. Linux在 ...

  2. php 第三方DB库NOTORM

    百度NOTORM找到该库的官网 :http://www.notorm.com/ 打开E:\AppServ\php7\php.ini 找到extension=php_pdo_mysql.dll 解开前面 ...

  3. sphinx 配置文件全解析(转)

    sphinx的配置文件是在配置的时候最容易出错的了: 我们先要明白几个概念: source:数据源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一 ...

  4. unity hide/show text

    using UnityEngine;using System.Collections; public class PlayerController:MonoBehaviour{    public U ...

  5. iOS: 让键盘消失的的4种方法

    转自:http://leopard168.blog.163.com/blog/static/168471844201422121310352/ 在iOS app中,只要用到编辑框(UITextFiel ...

  6. 利用yacc和lex制作一个小的计算器

    买了本<自制编程语言>,这本书有点难,目前只是看前两章,估计后面的章节,最近一段时间是不会看了,真的是好难啊!! 由于本人是身处弱校,学校的课程没有编译原理这一门课,所以就想看这两章,了解 ...

  7. 每日英语:China's New Anti-Graft Website: A Tale of Tigers, Flies and Bath Tubs

    With considerable fanfare, China's anti-graft squad has rolled out a brand new website in the ongoin ...

  8. ny269 VF

    VF 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Vasya is the beginning mathematician. He decided to make ...

  9. 使用codedom 编写脚本解释器

    本篇博客的目的是为了保存例子,怕自己忘记. private void dd(string code) { string path = "BonkerSpace"; if (File ...

  10. electron 的窗口设置最大化 最小化

    /** * Created by Administrator on 2016/11/23. * 页面对窗口的一些操作封装,用于渲染进程 */ "use strict"; const ...