Uva_11468_substring_ (AC automata + probabilistic dynamic programming)

Source: Internet
Author: User
Tags alphanumeric characters

Describe

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 2463

Give some substrings. Then give some characters, and the probability of each character appearing. Now use these characters to form a string with a length of s, and ask what the probability of the substring not appearing in the string is not.

Analysis

Select the letter edge to match. As long as the preceding string does not match successfully. Create an AC automaton with the preceding substrings and run on it. Use the match array to indicate whether each point is a word node, according to test instructions, as long as it is not a word node that can run. Then the AC automaton is get_ Fail to make up for the lack of edge.

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxt= -+5, maxk= -+5, maxl= -+5, maxnode= -* -+5, type= -+ -+Ten;5 intt,k,l,n,kase;6 Charp[maxk][ -];7 DoublePro[type];8InlineintIdxCharc) {9     if(c>='0'&&c<='9')returnC-'0';Ten     if(c>='a'&&c<='Z')returnC-'a'+Ten; One     returnC-'A'+ $; A } - structaho_corasick{ -     intCh[maxnode][type]; the     intF[maxnode]; -     DoubleDP[MAXNODE][MAXL]; -     BOOLMatch[maxnode]; -     intsz; +     voidinit () { -sz=1; +match[0]=false; AMemset (Pro,0,sizeofPro); atmemset (ch[0],0,sizeofch[0]); -memset (dp,-1,sizeofDP); -     } -     voidInsertChar*s) { -         intu=0; -          for(; *s;s++){ in             intC=IDX (*s); -             if(!Ch[u][c]) { tomemset (Ch[++sz],0,sizeofCh[sz]); +match[sz]=false; -ch[u][c]=sz; the             } *u=Ch[u][c]; $         }Panax Notoginsengmatch[u]=true; -     } the     voidGet_fail () { +Queue <int>Q; Af[0]=0; the          for(intC=0; c<type;c++){ +             intu=ch[0][c]; -             if(u) {f[u]=0; Q.push (u);} $         } $          while(!Q.empty ()) { -             intR=Q.front (); Q.pop (); -              for(intC=0; c<type;c++){ the                 intu=Ch[r][c]; -                 if(!u) {Ch[r][c]=ch[f[r]][c];Continue; }Wuyi q.push (u); the                 intv=F[r]; -f[u]=Ch[v][c]; Wumatch[u]|=Match[f[u]]; -             } About         } $     } -     DoubleGet_pro (intUintl) { -         if(!l)return 1.0; -         if(dp[u][l]>=0)returnDp[u][l]; A         Double&ans=Dp[u][l]; +ans=0.0; the          for(intC=0; c<type;c++)if(!match[ch[u][c]]) Ans+=pro[c]*get_pro (ch[u][c],l-1); -         returnans; $     } the }ac; the intMain () { thescanf"%d",&t); the      while(t--){ - ac.init (); inscanf"%d",&k); the          for(intI=1; i<=k;i++){ thescanf"%s", P[i]); About Ac.insert (P[i]); the         } the Ac.get_fail (); thescanf"%d",&n); +          for(intI=1; i<=n;i++){ -             CharC; the              while(C=getchar (), c=='\ n');Bayiscanf"%LF",&Pro[idx (c)]); the         } thescanf"%d",&l); -printf"Case #%d:%lf\n", ++kase,ac.get_pro (0, L)); -     } the     return 0; the}
View Code

11468
Substring
Given a set of pattern strings, and a text, you has to find, if any of the pattern is a substring of the
Text. If any of the pattern string can is found in text and then print ' yes ', otherwise ' no ' (without quotes).
But, unfortunately, thats isn't what's 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'll is a blank line after each test case.
Output
For each test case, output the number of the 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-Z, a to-Z, 0 to 9)
valid characters is 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 automata + probabilistic dynamic programming)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.