UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机
/**
链接: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自动机的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- HUD 1501 Zipper(记忆化 or DP)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- HDU1978How Many Ways 记忆化dfs+dp
/*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- UVA 11884 A Shooting Game(记忆化搜索)
A and B are playing a shooting game on a battlefield consisting of square-shaped unit blocks. The bl ...
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...
- C#LeetCode刷题-记忆化
记忆化篇 # 题名 刷题 通过率 难度 329 矩阵中的最长递增路径 31.0% 困难
- 集训第五周动态规划 I题 记忆化搜索
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
随机推荐
- npm下载缓慢解决方法
npm的服务器在国外,拉取npm包的列表.下载包这个过程会比较缓慢.凡是包管理工具基本都有这个问题,例如maven.pip等,这些问题都可以通过配置镜像来解决.阿里巴巴提供了maven库,清华大学有p ...
- Python 绘图库的使用:matplotlib
Matplotlib 官方API地址:https://matplotlib.org/ 例子: import matplotlib.pyplot as plt num_list=[1.5,0.6,7.8 ...
- SVN标准开发布局目录,trunk,branches,tags用法详解
http://www.cnblogs.com/newstar/archive/2011/01/04/svn.html 关于 SVN 目录结构 Subversion有一个很标准的目录结构,是 ...
- 如何在win7下安装和配置Android Studio
下载地址:http://developer.android.com/sdk/installing/studio.html#download 如果出现启动不了的问题 进入Android Studio安装 ...
- X-Forwarded-For的一些理解
X-Forwarded-For 是一个 HTTP 扩展头部,主要是为了让 Web 服务器获取访问用户的真实 IP 地址(其实这个真实未必是真实的,后面会说到). 那为什么 Web 服务器只有通过 X- ...
- 在js或css后加?v= 版本号不让浏览器缓存
客户端会缓存css或js文件,改变版本号,客户端浏览器就会重新下载新的js或css文件,在js或css后加?v= 版本号的用法如下 代码如下: <span style="font-si ...
- iOS - App 应用
1.Xcode 项目属性 Product Name 软件名称.产品名称.项目名称 Organization Name 公司名称.组织名称 Organization Identifier 公司的唯一标识 ...
- 自动化测试尝试 动态Linq表达式生成 ftp上传
自动化测试尝试 1. Selenium IDE Selenium IDE is a Chrome and Firefox plugin which records and plays back u ...
- linux上创建PV/VG/LV
LVM的整体思路是: 首先创建PV-->然后创建VG并将多个PV加到VG里-->然后创建LV-->格式化分区-->mount分区 1.创建PV pvcreate /dev/sd ...
- Chrome 错误代码:ERR_UNSAFE_PORT
最近在用Nginx发布多个站点测试,使用了87.88端口, 88端口访问正常,87端口就怎么也访问不了, 点击更多,提示错误代码:ERR_UNSAFE_PORT 不安全的端口?尼玛就只靠端口就能解决不 ...