Simple dp+ large number of URAL 1158 AC automata

Source: Internet
Author: User

Main topic

In a language the alphabet has N (n<=50) letters, each word is composed of M (M<=50) letters, so that altogether can form n^m words. But it is forbidden to have P (p<=10) strings, which means that any substring of a word cannot contain any of these p-strings. Q. How many legal strings can be generated according to the above rules? For example: N=3 m=3 p=3 The three characters in the alphabet are qwe prohibited strings for "QQ", "WEE", "Q", then a total of 7 valid strings.

This topic is equivalent to the DP of the state of each point on the AC automaton by the number of steps

DP[I][J] means to reach I this point, walked J step existence how many ways

Always push from the previous step to the next

It doesn't matter if you write a scrolling array.

It is important to note that in the case of an AC automaton update operation, the fail position at the end of each string will be marked, with explanatory notes

1#include <cstdio>2#include <cstring>3#include <queue>4#include <vector>5#include <iostream>6#include <algorithm>7#include <map>8 using namespacestd;9 #defineCLR (x) memset (x, 0, sizeof (x))Ten #defineSet (x) memset (x,-1, sizeof (x)) OnetypedefLong LongLL; A #defineRep (I, A, b) for (int i = A; I < b; + + i) - #defineFor (I, A, b) for (int i = A; I <= B; + + i) - #defineRev (I, A, b) for (int i = A; I >= b;---I) themap<Char,int>MP; -  - Const intChar_size =Wuyi; - Const intMax_size = the; + Const intM =10000 ; -  intn,m,p; +  A structBigInt { at     intWei, a[ -]; - BigInt () { -Wei =1; -Memset (A,0,sizeof(a)); -     } -     voidinit () { inWei =1; -Memset (A,0,sizeof(a)); to     } +     voidprint () { -          for(inti=wei-1; i>=0; i--) printf ("%d", A[i]); theprintf"\ n"); *     } $BigIntoperator+(BigInt m) {Panax Notoginseng BIGINT ret; -         intMa =Max (Wei, m.wei); the          for(intI=0; I<ma; i++){ +Ret.a[i] + = a[i]+M.a[i]; A             if(ret.a[i]>=Ten) ret.a[i+1]++,ret.a[i]-=Ten; the         } +         if(Ret.a[ma]) Ret.wei = ma+1; -         ElseRet.wei =Ma; $         returnret; $     } - }; -  the structac_machine{ -     intCh[max_size][char_size], val[max_size], fail[max_size];Wuyi     intsz; the  -     voidinit () { WuSZ =1; -CLR (ch[0]), CLR (Val); About     } $  -     voidInsertChar*s) { -         intn =strlen (s); -         intu=0 ; A          for(intI=0; I<n; i++){ +             intc =Mp[s[i]]; the             if(!Ch[u][c]) { - CLR (Ch[sz]); $VAL[SZ] =0; theCH[U][C] = sz++; the             } theU =Ch[u][c]; the         } -Val[u] =1; in     } the  the     voidGet_fail () { Aboutqueue<int>Q; thefail[0] =0; the          for(intC=0; C<n; C++){ the             intU = ch[0][c]; +             if(u) {Q.push (u); fail[u]=0;} -         } the          while(!Q.empty ()) {Bayi             intR =Q.front (); the Q.pop (); the             //For example, it is illegal for history to be in history, and it is illegal for history to access its S. -VAL[R] |=Val[fail[r]]; -              for(intC=0; C<n; C++){ the                 intU =Ch[r][c]; the                 if(!u) {Ch[r][c] = Ch[fail[r]][c];Continue;} theFail[u] =Ch[fail[r]][c]; the q.push (u); -             } the         } the     } the }ac;94  the Charstr[155]; theBigInt dp[max_size][155]; the 98BigInt Solve (intNintStep) About { -      for(intI=0; I&LT;AC.SZ; i++)101          for(intj=0; J<=step; J + +){102 dp[i][j].init ();103         }104dp[0][0].wei = dp[0][0].a[0] =1; the      for(intI=1; I<=step; i++){106          for(intj=0; J&LT;AC.SZ; J + +){107              for(intk=0; K<n; k++)108                 if(!Ac.val[ac.ch[j][k]]) {109Dp[ac.ch[j][k]][i] = dp[ac.ch[j][k]][i]+dp[j][i-1]; the                 }111         } the     }113BIGINT ret =BigInt (); the      for(intj=0; J&LT;AC.SZ; J + +) ret = ret+Dp[j][step]; the     returnret; the }117 118 intMain ()119 { -    //freopen ("In.txt", "R", stdin);121    //freopen ("OUT.txt", "w", stdout);122      while(~SCANF ("%d%d%d", &n, &m, &p)) {123 mp.clear ();124 GetChar (); the gets (str);126          for(intI=0; I<strlen (str); i++) Mp[str[i] =i;127 ac.init (); -          for(intI=1; I<=p; i++){129 gets (str); the Ac.insert (str);131         } the Ac.get_fail ();133BIGINT ret =solve (n, m);134 ret.print ();135     }136     return 0;137}

Simple dp+ large number of URAL 1158 AC automata

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.