"Uva11468-substring" AC automatic machine +DP

Source: Internet
Author: User

http://acm.hust.edu.cn/vjudge/problem/31655

Test instructions: Given the k template string, n characters and the probability of choosing it pro[i], to construct a length to ask L the string s, ask s does not contain any of the probability of a template string.

Exercises

Ed[i] Mark the point I on trie is not the end of any template string (Ed[i]|=ed[i.fail when seeking fail)
D[i][l] means starting from I also take the L-step, the structure of the string without the probability of template string, DP a bit. Notice that zero is cleared.

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <queue>6 using namespacestd;7 8 Const intn= -, l= the, s= the;9 CharS[l];Ten intcnt[n*L]; One Doubled[n*l][l],pro[ -]; A BOOLed[n*L]; -queue<int>Q; - intnum,k,n,l; the structnode{ -     intson[ -]; -     intfail; -}a[n*L]; +  - intIdxCharc) + { A     if(c<='Z'&& c>='a')returnC-'a'+1; at     if(c<='Z'&& c>='A')returnC-'A'+ -; -     returnC-'0'+ -; - } -  - voidClearintx) - { inA[x].fail=0; -memset (A[x].son,0,sizeof(A[x].son)); to } +  - voidTrieChar*c) the { *     intL=strlen (c); $     intx=0;Panax Notoginseng      for(intI=0; i<l;i++) -     { the         intt=idx (c[i]); +         if(!A[x].son[t]) A         { thenum++; + Clear (num); -a[x].son[t]=num; $         } $x=A[x].son[t]; -     } -ed[x]=1; the } - Wuyi voidBuildac () the { -      while(!q.empty ()) Q.pop (); Wu      for(intI=1; i<=s;i++) -         if(a[0].son[i]) Q.push (a[0].son[i]); About      while(!q.empty ()) $     { -         intx=Q.front (); Q.pop (); -         intFail=A[x].fail; -          for(intI=1; i<=s;i++) A         { +             inty=A[x].son[i]; the             if(y) -             { $A[y].fail=A[fail].son[i]; theed[y]|=Ed[a[fail].son[i]]; the Q.push (y); the             } the             Elsea[x].son[i]=A[fail].son[i]; -         } in     } the } the  About DoubleCountintXintl) the { the     if(l==0)returnd[x][l]=1.0; the     if(d[x][l]!=-1)returnD[x][l]; +d[x][l]=0; -      for(intI=1; i<=s;i++) the     {Bayi         if(!pro[i])Continue; the         inty=A[x].son[i]; the         if(!ed[y]) D[x][l]+=pro[i]*count (y,l-1); -     } -     returnD[x][l]; the } the  the intMain () the { -Freopen ("a.in","R", stdin); theFreopen ("a.out","W", stdout); the     intt,cas=0; thescanf"%d",&T);94      while(t--) the     { thenum=0; theClear0);98memset (CNT,0,sizeof(CNT)); Aboutmemset (Ed,0,sizeof(ed)); -Memset (Pro,0,sizeof(pro));101scanf"%d",&k);102          for(intI=1; i<=k;i++)103         {104scanf"%s", s); the Trie (s);106         }107 Buildac ();108scanf"%d",&n); GetChar ();109          for(intI=1; i<=n;i++) the         {111             CharC; thescanf"%c",&c); GetChar ();113scanf"%LF",&Pro[idx (c)]); GetChar (); the         } thescanf"%d",&l); the          for(intI=0; i<=num;i++)117              for(intj=0; j<=l;j++)118d[i][j]=-1;119printf"Case #%d:%lf\n", ++cas,count (0, L)); -     }121     return 0;122}

"Uva11468-substring" AC automatic machine +DP

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.