UVA 11468 Substring

Source: Internet
Author: User

Test instructions: give you k a template string, then give you some probability of the occurrence of characters, then give you a length l, ask you these characters are composed of the length of the string does not contain any one template string probability.

Idea: AC automata + Introduction DP

First, a good AC automaton is constructed with K templates. The topic said that the new string of long L does not contain any K-string, in fact, is said in the structure of the tree, from the root down the L step does not contain K templates. Whether the question is a K template string with the match tag.

The state transfer equation code is commented out.

1#include <cstdio>2#include <cstring>3#include <queue>4#include <cstdio>5#include <map>6#include <string>7 using namespacestd;8 9 Const intSigma_size = -;Ten Const intMaxnode = -;//Total node points One Const intMaxs = -+Ten;//Number of templates A  - intidx[ the], N; - DoubleProb[sigma_size]; the  - structAhocorasickautomata - { -     intCh[maxnode][sigma_size]; +     intF[maxnode];//fail function -     intMatch[maxnode];//whether to include a string +     intSz//Total node points A  at     voidInit () -     { -SZ =1; -memset (ch[0],0,sizeof(ch[0])); -     } -  in     //Insert String -     voidInsertChar*s) to     { +         intU =0, n =strlen (s); -          for(inti =0; I < n; i++) the         { *             intc =Idx[s[i]]; $             if(!Ch[u][c])Panax Notoginseng             { -memset (Ch[sz],0,sizeof(Ch[sz])); theMATCH[SZ] =0; +CH[U][C] = sz++; A             } theU =Ch[u][c]; +         } -Match[u] =1; $     } $  -     //calculate the Fail function -     voidGetfail () the     { -queue<int>Q;Wuyif[0] =0; the         //Initialize Queue -          for(intc =0; c < sigma_size; C++) Wu         { -             intU = ch[0][c]; About             if(U) $             { -F[u] =0; - q.push (u); -             } A         } +         //fail calculation by BFS sequence the          while(!q.empty ()) -         { $             intR =Q.front (); the Q.pop (); the              for(intc =0; c < sigma_size; C++) the             { the                 intU =Ch[r][c]; -                 if(!u) in                 { theCH[R][C] =Ch[f[r]][c]; the                     Continue; About                 } the q.push (u); the                 intv =F[r]; the                  while(v &&!ch[v][c]) v =F[v]; +F[u] =Ch[v][c]; -Match[u] |=Match[f[u]]; the             }Bayi         } the     } the }; -  - ahocorasickautomata ac; the  the Doubled[maxnode][ the]; the intvis[maxnode][ the]; the  - DoubleGetprob (intUintL//D[u][l]=prob[u]*d[v][l-1] state-to-equation V for u son can walk node the { the     if(! Lreturn 1.0; the     if(Vis[u][l])94         returnD[u][l]; theVIS[U][L] =1; thed[u][l]=0.0; the      for(inti =0; I < n; i++)98         if(!Ac.match[ac.ch[u][i]]) AboutD[u][l] + = prob[i] * Getprob (Ac.ch[u][i], L-1); -     returnD[u][l];101 }102 103 Chars[ -][ -];104  the intMain ()106 {107     intT;108scanf"%d", &T);109      for(intKase =1; Kase <= T; kase++) the     {111         intK, L; thescanf"%d", &k);113          for(inti =0; I < K; i++) scanf ("%s", S[i]); thescanf"%d", &n); the          for(inti =0; I < n; i++) the         {117             Charch[9];118scanf"%S%LF", CH, &prob[i]);119idx[ch[0]] =i; -         }121 ac.init ();122          for(inti =0; I < K; i++) Ac.insert (S[i]);123 Ac.getfail ();124scanf"%d", &L); thememset (Vis,0,sizeof(Vis));126memset (D,0,sizeof(d));127printf"Case #%d:%.6lf\n", Kase, Getprob (0, L)); -     }129     return 0; the}
View Code

UVA 11468 Substring

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.