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自动机, ...
随机推荐
- Linux内核Radix Tree(三):API介绍
1. 单值查找radix_tree_lookup 函数radix_tree_lookup执行查找操作,查找方法是:从叶子到树顶,通过数组索引键值值查看数组元素的方法,一层层地查找slot.其列 ...
- C#基础(七)——静态类与非静态类、静态成员的区别
静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 new 关键字创建静态类类型的变量.在声明一个类时使用static关键字,具有两个方面的意义:首先,它防止程序员写代码来实例 ...
- post提交与get提交的一个小知识点
今天偶然发现post提交与get提交的一个小知识点,记下来以后可以看看. 将form表单的method的设置为get后,通过url传递的参数将不会被传递到服务器,例如1.ashx?a=123,的a=1 ...
- Cassandra1.2文档学习(5)—— Snitch
参考资料:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...
- err: exp-00056 rra-12154 exp-00000
一直用的是win7系统+oracle11g r1 server端(后简称r1). 由于工作需要装了oracle11g r2 client端(后简称r2). 在装r2之前,r1的导出exp是没有问题的. ...
- Java遍历所有网卡打印对应IP
import java.util.Enumeration; import java.net.*; public class Test { /** * @param args */ public sta ...
- 解决lucene 重复索引的问题
在使用Lucene过程中,会发现当我们为添加新的Document时,会产生重复现象(两次添加同一个Document),毕竟Lucene中没有像数据库中一样,有键可以区分.不过我们可以通过为Docume ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
- vs2013 上传碰到的问题:“输入的不是有效的 Base-64 字符串 ”
action 代码: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create( ImageStoreModels images ...
- gradient css
<!DOCTYPE html> <html> <head> <title></title> <script src="js/ ...