/**
链接: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. python练习笔记——计算1/1-1/3+1/5-1/7……的和

    1 / 1 - 1 / 3 + 1 / 5 - 1 / 7 + ....求100000个这样的分式计算之为是多少?将此值乘以4后打印出来,看看是什么? num_list = [] count = -1 ...

  2. 使用Xcode、Android Studio将项目链接到Git

    一.使用Android Studio创建本地git仓库: 1.检查本地git环境:在Android Studio中setting-->Version Control 点击Test按钮,提示suc ...

  3. go 学习笔记(4) ---项目结构

    go install和go build之争.目前,IDEA插件和LiteIDE都采用了go build.Eclipse插件采用了go install.官方推荐go install方式编译项目,官方项目 ...

  4. AP_自动付款工作台设定和操作(流程)

    2014-06-04 Created By BaoXinjian

  5. Linux内核(4) - 内核学习的心理问题

    对于学习来说,无论是在学校的课堂学习,还是这里说的内核学习,效果好或者坏,最主要取决于两个方面——方法论和心理.注意,我无视了智商的差异,这玩意儿玄之又玄,岔开了说,属于迷信的范畴. 前面又是Kern ...

  6. OpenCV245之SURF源代码分析

    一.fastHessianDetector函数分析 (1)參数 const Mat& sum                积分图片 const Mat& mask_sum vecto ...

  7. mysql 再查询结果的基础上查询(子查询)

    SELECT A.wx_name, A.wx_litpic, B . * FROM ( SELECT uid, COUNT( * ) AS daticishu FROM statements , ) ...

  8. unity update与fixedUpdate

    update与渲染同步.fixedUpdate与物理同步. 在update中,speed要乘以Time.deltaTime.在fixedUpdate中,speed要乘以Time.fixedDeltaT ...

  9. php分割字符串方法速度比較(substr/sscanf/preg_match)

    固定長度的字串(假設是 06481a63041b578d702f159f520847f8), 要照固定格式做切割, 使用 PHP 要怎麼切會比較快? 註: 要將此字串切成 => 06 / 48 ...

  10. CDH5.2+CM5.2+impala2+Spark1.1 集群搭建基础环境准备

    測试集群简单介绍:一共同拥有4台机器:10.10.244.136.10.10.244.137.10.10.244.138.10.10.244.139. 10.10.244.136是管理节点.另外3台是 ...