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自动机, ...
随机推荐
- C语言遍历一个文件夹下面的所有文件
主要用到的函数/function. These should get you started: opendir() readdir() closedir() fopen() fread() fwrit ...
- Linux用户级线程和内核级线程区别
1.内核级线程: (1)线程的创建.撤销和切换等,都需要内核直接实现,即内核了解每一个作为可调度实体的线程.(2)这些线程可以在全系统内进行资源的竞争.(3)内核空间内为每一个内核支持线程设置了一个线 ...
- Eclipse启动报错Java was started but returned exit code=13
启动Eclipse的时候报错Java was started but returned exit code=13,这个错误的原因是由于eclipse版本与jdk版本不符导致的,可能你的eclipse是 ...
- PHP 各种函数
usleep() 函数延迟代码执行若干微秒. unpack() 函数从二进制字符串对数据进行解包. uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID. time_sleep_unti ...
- jquery checkbox 选中 全选 插件
checkbox 选中 全选 在项目中经常用到,但是不同的程序员写出的东西各有差异,在此整合了jquery checkbox插件,用起来很方便,也总结了我们项目中通常会出现问题的地方,一行代码搞定. ...
- 阿里云服务器centos5.10安装lamp环境
==相关命令== 查看linux版本:cat /etc/redhat-release ==配置修改== 一.Apache配置 ------------------------------------- ...
- java实现的JDBCTemplate工具
1.DButil import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; impor ...
- mysql UNIX时间戳与日期的相互转换 查询表信息
UNIX时间戳转换为日期用函数FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时间戳用函数UNIX_TIMESTAMP() Sel ...
- 【viewResolver】 springmvc jsp
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < ...
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7335 Accepted: 2878 Descri ...