描述


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自动机+概率动态规划)的更多相关文章

  1. 【BZOJ1030】文本生成器(AC自动机,动态规划)

    [BZOJ1030]文本生成器(AC自动机,动态规划) 题面 BZOJ 题解 超级简单良心送分题 很明显是所有状态-不合法状态 合法状态就是\(26^m\) 不合法状态做一个\(dp\)就好 #inc ...

  2. 【BZOJ3530】数数(AC自动机,动态规划)

    [BZOJ3530]数数(AC自动机,动态规划) 题面 BZOJ 题解 很套路的\(AC\)自动机+\(DP\) 首先,如果长度小于\(N\) 就不存在任何限制 直接大力\(DP\) 然后强制限制不能 ...

  3. 【BZOJ1559】[JSOI2009]密码(AC自动机,动态规划,搜索)

    [BZOJ1559][JSOI2009]密码(AC自动机,动态规划,搜索) 题面 BZOJ 洛谷 题解 首先求方案数显然是构建\(AC\)自动机之后再状压\(dp\),似乎没有什么好讲的. 现在考虑答 ...

  4. UVa 11468 (AC自动机 概率DP) Substring

    将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...

  5. BZOJ2553[BeiJing2011]禁忌——AC自动机+概率DP+矩阵乘法

    题目描述 Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi恢复了读心的能力…… 如今,在John已经成为传 ...

  6. UVA11468 Substring --- AC自动机 + 概率DP

    UVA11468 Substring 题目描述: 给定一些子串T1...Tn 每次随机选择一个字符(概率会给出) 构造一个长为n的串S,求T1...Tn不是S的子串的概率 直接把T1...Tn建成AC ...

  7. 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 ...

  8. 【BZOJ1444】[Jsoi2009]有趣的游戏 AC自动机+概率DP+矩阵乘法

    [BZOJ1444][Jsoi2009]有趣的游戏 Description Input 注意 是0<=P Output Sample Input Sample Output HINT  30%的 ...

  9. UVa 11468 Substring (AC自动机+概率DP)

    题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...

随机推荐

  1. centos 7下配置mysql+php(ThinkPHP)+nginx

    最近在Linux平台上配置服务器部署网站(说多了都是泪!),记个笔记! 一.首先是在centos下安装mysql (参考博客) mysql yum库提供了一个简单的和方便的方法来安装和更新MySQL相 ...

  2. lucene4.0 基于smb文件服务器的全文检索

    使用lucene 4.0版本的全文检索 所需要的jar包  网速太慢,下次有空再把jar传上来 1.FileIndex  建立索引,查询,删除,更新 package com.strongit.tool ...

  3. [leetcode] 407. Trapping Rain Water II

    https://leetcode.com/contest/6/problems/trapping-rain-water-ii/ 看到这题,我很高兴,因为我做过!哈哈!其实我现在也写不出来,知道大概思想 ...

  4. OpenJudge/Poj 1251 丛林中的路/Jungle Roads

    1.链接地址: http://bailian.openjudge.cn/practice/1251/ http://poj.org/problem?id=1251 2.题目: 总时间限制: 1000m ...

  5. 模板:LCS(最长公共子序列)

    #include <cstring> #define max(a,b) ((a) > (b) ? (a) : (b)) int same(char ch1,char ch2) { ; ...

  6. Unity3D--学习太空射击游戏制作(一)

    近期买了本书在学习一些Unity3D的东西,在了解了Unity3D工具的基本面板后开始学习一个太空射击游戏的开发过程. 首先下载一个关于本游戏的资源文件,(百度云下载地址:http://pan.bai ...

  7. 推荐一款好用的java反编译软件——JavaDecompiler

    这款反编译器叫 "Java Decompiler",在网上也是久负盛名,最近因为工作需要找来用了下,果然不错,之前都是用eclipse的插件jad来看源码的.下面这个链接是Java ...

  8. python27读书笔记0.2

    # -*- coding:utf-8 -*- ##s.partition(d)##Searches string s for the first occurrence of some delimite ...

  9. Project not selected to build for this solution configuration.

    Project not selected to build for this solution configuration.   When you upgrade your older solutio ...

  10. hive 存储格式

    hive有textFile,SequenceFile,RCFile三种文件格式. textfile为默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理. Seq ...