BZOJ1030 JSOI2007 Text Generator

Source: Internet
Author: User

AC automata.

F[I][J] is expressed as the number of scenarios in the text string that match the J node in the dictionary tree for length i.

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 Const intn=6005, mod=10007;5ll ans1,ans2,f[ the][n];6 Chars[ the];intcnt,n,m;7queue<int>Q;8 structnode9 {Ten     ints,v[ -],f; One }t[n]; A voidbuild () - { -     intN=strlen (s);intnow=0; the      for(intI=0; i<n;++i) -     { -         if(t[now].v[s[i]-'A']) now=t[now].v[s[i]-'A']; -         Else{ +t[now].v[s[i]-'A']=++cnt;now=CNT; -         } +     } At[now].s=1; at } - voidGetfail () - { -      for(intI=0;i< -;++i) -     { -         if(t[0].v[i]) in         { -t[t[0].v[i]].f=0; Q.push (t[0].v[i]); to         } +     } -      while(!q.empty ()) the     { *         intx=Q.front (); Q.pop (); $          for(intI=0;i< -;++i)Panax Notoginseng         { -             if(T[x].v[i]) the             { +t[t[x].v[i]].f=T[t[x].f].v[i];q.push (T[x].v[i]); A             } the             Elset[x].v[i]=T[t[x].f].v[i]; +t[t[x].v[i]].s|=T[t[t[x].v[i]].f].s; -         } $     } $ } - voidWork () - { thef[0][0]=1; -      for(intI=1; i<=m;++i)Wuyi          for(intj=0; j<=cnt;++j) { the             if(T[J].S)Continue; -              for(intk=0;k< -;++k) WuF[i][t[j].v[k]]= (f[i][t[j].v[k]]+f[i-1][J])%MoD; -         } Aboutans1=1; $      for(intI=0; i<=cnt;++i)if(!T[I].S) ans2= (Ans2+f[m][i])%MoD; -      for(intI=1; i<=m;++i) ans1=ans1* -%MoD; -printf"%lld\n", (ans1-ans2+mod)%MoD); -     return; A } + intMain () the { -scanf"%d%d",&n,&m); $      for(intI=1; i<=n;++i) thescanf"%s", s), build (); the Getfail (); the Work (); the     return 0; -}

BZOJ1030 JSOI2007 Text Generator

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.