[JSOI2007] Text generator (AC automaton +DP)

Source: Internet
Author: User

Test instructions

Give you a string of N. Ask how many strings of length m make the n strings appear at least once. Output answer membrane 10007 in the sense of the result.

(n<=100, length of each string <=100)

Exercises

Run DP on the AC automaton.

The idea of a tolerant thought, that the number of occurrences at least once was, all possible-the number of times that did not appear at all.

So consider DP, for a string of length I is transferred from I-1, the number of I positions must not be the end of a string in n strings.

So build an AC automaton and run on it. When a son transfers from his father, he must make sure that the point that the son node and the son node reach indirectly through the fail pointer is not the terminating node.

See the code specifically.

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cmath>5#include <algorithm>6#include <queue>7 using namespacestd;8 Const intMod=10007;9 Const intn=7000;Ten intN,m,tot,ans; One CharS[n]; A intdp[ $][n]; - structac{ -     intnxt[ -],e,fail; the }ac[n]; - voidInsertChars[]) { - //cout<< "Asfsfsdfsdfs" <<endl; -     intlen=strlen (s); +     intnow=0; -      for(intI=0; i<len;i++){ +         if(ac[now].nxt[s[i]-'A']==0) ac[now].nxt[s[i]-'A']=++tot; Anow=ac[now].nxt[s[i]-'A']; at     } -Ac[now].e=1; - } - voidGet_fail () { -queue<int>Q; -      for(intI=0; i<= -; i++){ in         if(ac[0].nxt[i]) { -         //cout<< "SFSD" <<endl; toQ.push (ac[0].nxt[i]); +         } -     } the      while(!Q.empty ()) { *          $         intU=q.front ();//cout<<u<<endl;Panax Notoginseng Q.pop (); -          for(intI=0; i<= -; i++){ the             if(Ac[u].nxt[i]) { +Ac[ac[u].nxt[i]].fail=Ac[ac[u].fail].nxt[i]; Aac[ac[u].nxt[i]].e|=ac[ac[ac[u].fail].nxt[i]].e; the Q.push (Ac[u].nxt[i]); +             } -             Elseac[u].nxt[i]=Ac[ac[u].fail].nxt[i]; $         } $     } - } - intMain () { thescanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++){Wuyiscanf"%s", s); the insert (s); -     } Wu Get_fail (); -dp[0][0]=1; About      for(intI=1; i<=m;i++) $          for(intj=0; j<=tot;j++) -              for(intx=0; x<= -; x + +){ -                 if(ac[ac[j].nxt[x]].e==0) dp[i][ac[j].nxt[x]]= (dp[i][ac[j].nxt[x]]+dp[i-1][J])%MoD; -             } A     intans=1; +      for(intI=1; i<=m;i++){ theans*= -; -ans%=MoD; $     } the      for(intI=0; i<=tot;i++){ theans-=Dp[m][i]; theans= (ans+mod)%MoD; the     } -printf"%d", ans); in     return 0; the}

[JSOI2007] Text generator (AC automaton +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.