UVA_11468_Substring_(AC自动机+概率动态规划)
描述
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2463
给出一些子串.然后给出一些字符,以及每个字符出现的概率.现在用这些字符组成一个长度为s的字符串,问之前给出的子串都没有在这个字符串中出现的概率是多少.
分析
边选字母边匹配.只要前面的字串都不能匹配成功即可.用前面的那些子串造出个AC自动机,然后在上面跑.用match数组表示每个点是否是单词节点,根据题意,只要不是单词节点都可以跑.然后AC自动机在get_fail的时候把没有的边补上.
#include <bits/stdc++.h>
using namespace std; const int maxt=+,maxk=+,maxl=+,maxnode=*+,type=++;
int t,k,l,n,kase;
char p[maxk][];
double pro[type];
inline int idx(char c){
if(c>=''&&c<='') return c-'';
if(c>='a'&&c<='z') return c-'a'+;
return c-'A'+;
}
struct Aho_Corasick{
int ch[maxnode][type];
int f[maxnode];
double dp[maxnode][maxl];
bool match[maxnode];
int sz;
void init(){
sz=;
match[]=false;
memset(pro,,sizeof pro);
memset(ch[],,sizeof ch[]);
memset(dp,-,sizeof dp);
}
void insert(char *s){
int u=;
for(;*s;s++){
int c=idx(*s);
if(!ch[u][c]){
memset(ch[++sz],,sizeof ch[sz]);
match[sz]=false;
ch[u][c]=sz;
}
u=ch[u][c];
}
match[u]=true;
}
void get_fail(){
queue <int> q;
f[]=;
for(int c=;c<type;c++){
int u=ch[][c];
if(u){ f[u]=; q.push(u); }
}
while(!q.empty()){
int r=q.front(); q.pop();
for(int c=;c<type;c++){
int u=ch[r][c];
if(!u){ ch[r][c]=ch[f[r]][c]; continue; }
q.push(u);
int v=f[r];
f[u]=ch[v][c];
match[u]|=match[f[u]];
}
}
}
double get_pro(int u,int l){
if(!l) return 1.0;
if(dp[u][l]>=) return dp[u][l];
double &ans=dp[u][l];
ans=0.0;
for(int c=;c<type;c++)if(!match[ch[u][c]]) ans+=pro[c]*get_pro(ch[u][c],l-);
return ans;
}
}ac;
int main(){
scanf("%d",&t);
while(t--){
ac.init();
scanf("%d",&k);
for(int i=;i<=k;i++){
scanf("%s",p[i]);
ac.insert(p[i]);
}
ac.get_fail();
scanf("%d",&n);
for(int i=;i<=n;i++){
char c;
while(c=getchar(),c=='\n');
scanf("%lf",&pro[idx(c)]);
}
scanf("%d",&l);
printf("Case #%d: %lf\n",++kase,ac.get_pro(,l));
}
return ;
}
11468
Substring
Given a set of pattern strings, and a text, you have to find, if any of the pattern is a substring of the
text. If any of the pattern string can be found in text, then print ‘yes’, otherwise ‘no’ (without quotes).
But, unfortunately, thats not what is asked here.
The problem described above, requires a input file generator. The generator generates a text of
length L, by choosing L characters randomly. Probability of choosing each character is given as priori,
and independent of choosing others.
Now, given a set of patterns, calculate the probability of a valid program generating “no”.
Input
First line contains an integer T , the number of test cases. Each case starts with an integer K, the
number of pattern strings. Next K lines each contain a pattern string, followed by an integer N ,
number of valid characters. Next N lines each contain a character and the probability of selecting that
character, p i . Next an integer L, the length of the string generated. The generated text can consist of
only the valid characters, given above.
There will be a blank line after each test case.
Output
For each test case, output the number of test case, and the probability of getting a “no”.
Constraints:
• T ≤ 50
• K ≤ 20
• Length of each pattern string is between 1 and 20
• Each pattern string consists of only alphanumeric characters (a to z, A to Z, 0 to 9)
• Valid characters are all alphanumeric characters
•
∑
p i = 1
• L ≤ 100
Sample Input
2
1
a
2
a 0.5
b 0.5
2
2
ab
2
a 0.2
b 0.8
2
Sample Output
Case #1: 0.250000
Case #2: 0.840000
UVA_11468_Substring_(AC自动机+概率动态规划)的更多相关文章
- 【BZOJ1030】文本生成器(AC自动机,动态规划)
[BZOJ1030]文本生成器(AC自动机,动态规划) 题面 BZOJ 题解 超级简单良心送分题 很明显是所有状态-不合法状态 合法状态就是\(26^m\) 不合法状态做一个\(dp\)就好 #inc ...
- 【BZOJ3530】数数(AC自动机,动态规划)
[BZOJ3530]数数(AC自动机,动态规划) 题面 BZOJ 题解 很套路的\(AC\)自动机+\(DP\) 首先,如果长度小于\(N\) 就不存在任何限制 直接大力\(DP\) 然后强制限制不能 ...
- 【BZOJ1559】[JSOI2009]密码(AC自动机,动态规划,搜索)
[BZOJ1559][JSOI2009]密码(AC自动机,动态规划,搜索) 题面 BZOJ 洛谷 题解 首先求方案数显然是构建\(AC\)自动机之后再状压\(dp\),似乎没有什么好讲的. 现在考虑答 ...
- UVa 11468 (AC自动机 概率DP) Substring
将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...
- BZOJ2553[BeiJing2011]禁忌——AC自动机+概率DP+矩阵乘法
题目描述 Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi恢复了读心的能力…… 如今,在John已经成为传 ...
- UVA11468 Substring --- AC自动机 + 概率DP
UVA11468 Substring 题目描述: 给定一些子串T1...Tn 每次随机选择一个字符(概率会给出) 构造一个长为n的串S,求T1...Tn不是S的子串的概率 直接把T1...Tn建成AC ...
- 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元
http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...
- 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法
[BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT 30%的 ...
- UVa 11468 Substring (AC自动机+概率DP)
题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...
随机推荐
- tomcat根据繁忙线程数对keepalive进行动态调整
众所周知,Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接.我们经常所用的tomcat服务器就支持HTTP Ke ...
- HTML5新增标签属性
----- 新类型表单 - email 自动校验输入的是不否是email 邮箱:<input type="email" name="user_email" ...
- Openstack安装
作者:陈沙克 Openstack发展很猛,很多朋友都很认同,2013年,会很好的解决OpenStack部署的问题,让安装,配置变得更加简单易用. 很多公司都投入人力去做这个,新浪也计划做一个Opens ...
- php 中 isset()函数 和 empty()函数的区别
首先这两个函数都是用来测试变量的状态: isset()函数判断一个变量是否在 如果存在返回true 否则返回false empty()函数判断一个变量是否为空,如果为空返回true 否则返回fals ...
- 用LinqToExcel处理有标题表格的数据
1. 先根据表格标题定义一个类. public class News { public string Title { set; get; } public string Content { set; ...
- python正则实例
# -*- coding: cp936 -*-import reidcardregex=r"^[1-9]\d{14}(\d{2}[0-9x])?$"print re.search( ...
- iis7如何取消目录的可执行权限
我们需要把IIs中某一个目录的可执行权限去掉.这在IIs6中是非常方便的,可是因为iis7的机制小编也找了不少资料才找到. 第一步:先选择需要取消权限的目录,然后在右边可以看到 “处理程序映射” 双击 ...
- controller.pp 各组件的安装顺序
controller 属性: admin_address => $controller_node_address, public_address => $c ...
- LINQ实战
转载:http://www.cnblogs.com/yubinfeng/p/4567064.html 原作者:尼古拉斯~yu 文章部分内容在原文的基础上有删改. 我相信在net进阶的这个阶段,很 ...
- MVC-登录并设置角色
1.新建一个类,设置角色: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...