/**
链接: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. 【LeetCode】9. Palindrome Number (2 solutions)

    Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...

  2. 批量修改Java类文件中引入的package包路径

    http://libeey.blogbus.com/logs/101848958.html当复制其他工程中的包到新工程的目录中时,由于包路径不同,出现红叉,下面的类要一个一个修改包路径,类文件太多的话 ...

  3. Kafka日志清除策略

    一.更改日志输出级别 config/log4j.properties中日志的级别设置的是TRACE,在长时间运行过程中产生的日志大小吓人,所以如果没有特殊需求,强烈建议将其更改成INFO级别.具体修改 ...

  4. FA_资产成批调整(流程)

    2014-06-08 Created By BaoXinjian

  5. 基于UDP协议的网络程序

    一.下图是典型的UDP客户端/服务器通讯过程 下面依照通信流程,我们来实现一个UDP回射客户/服务器 #include <sys/types.h>  #include <sys/so ...

  6. IDLE经常使用快捷键汇总

    IDLE(An Integrated DeveLopment Environment for Python)是Python自带的编译器,在刚開始学习的人,或写小程序,或用于验证的时候,经经常使用到!假 ...

  7. ELK系统搭建流程

    ELK系统搭建流程 1. Logstash 1.1 安装 注:安装在需要收集日志的机器上. cd /data/softs sudo wget https://download.elastic.co/l ...

  8. mysql - 语法复习与学习

    //本月的第一天,最后一天 $start=date('Y-m-01', strtotime(date("Y-m-d"))); echo date('Y-m-d', strtotim ...

  9. python标准库介绍——9 copy模块详解

    ==copy 模块== ``copy`` 模块包含两个函数, 用来拷贝对象, 如 [Example 1-64 #eg-1-64] 所示. ``copy(object) => object`` 创 ...

  10. 配置新服务器 的一些 依赖库 php mysql nginx

    利用CentOS Linux系统自带的yum命令安装.升级所需的程序库 LANG=C yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel ...