Large number of dp+ on POJ--1625CENSORED!+AC automatic machine

Source: Internet
Author: User

Topic Link: Click to enter
In fact, it seems that the matrix can be completely done, but because of the use of large numbers, causing memory to open, so with DP write. In fact, the process of DP is still in the trie we use to prohibit the construction of words to walk the process of M-step. We define DP[I][J] to indicate the number of scenarios that reach node J after I step, then the state transition should be dp[i][j]=sum (Dp[i-1][k]), where k represents a node that can walk to J and cannot be a virus node. But in fact this code is not so good to write, in fact, we can use the node J Active to update its sub-node K, so the transfer equation becomes dp[i][next[j][k]]+=dp[i-1][j]. Require next[j][k] cannot make a virus node. All in all, the DP on the AC automaton is to use the trie tree we have built and the above transfer mode for state transfer.

The code is as follows:

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <algorithm>#include <map>using namespace STD;// large number of templatesstructbiginteger{inta[ -];enum{MOD =10000}; BigInteger () {memsetA0,sizeof(A)); a[0]=1;}void Set(intx) {memsetA0,sizeof(A)); a[0]=1; a[1]=x;}voidPrint () {printf("%d", a[a[0]]); for(inti=a[0]-1; I>0; i--) {if(a[i]==0){printf("0000");Continue;} for(intk=Ten; k*a[i]<mod; k*=Ten)printf("0");printf("%d", A[i]); }printf("\ n"); }int&operator[] (intP) {returnA[P];}Const int&operator[] (intPConst{returnA[P];} BigIntegeroperator+ (Constbiginteger& B) {BigInteger C; c[0]=max (a[0], b[0]); for(intI=1; i<=c[0]; i++) C[i]+=a[i]+b[i], c[i+1]+=c[i]/mod, C[i]%=mod;if(c[c[0]+1] >0) c[0]++;returnC } BigIntegeroperator* (Constbiginteger& B) {BigInteger C; c[0]=a[0]+b[0]; for(intI=1; i<=a[0]; i++) for(intj=1; j<=b[0]; J + +) {c[i+j-1]+=A[I]*B[J], c[i+j]+=c[i+j-1]/mod, c[i+j-1]%=mod; }if(c[c[0]] ==0) c[0]--;returnC }}one;Const intmaxn=Ten*Ten+5;CharALPH[MAXN]; Map<char,int>HintGetIndex (CharCH) {returnH[CH];}structtrie{intnext[maxn][maxn/2],FAIL[MAXN],FLAG[MAXN];intRoot,l,len;intNewNode () {memset(next[l],-1,sizeof(Next[l])); Flag[l++];returnL-1; }voidInit () {l=0;    Root=newnode (); }voidInsertCharBuf[]) {intlen1=strlen(BUF);intNow=root; for(intI=0; i<len1;i++) {intIndex=getindex (Buf[i]);if(next[now][index]==-1) Next[now][index]=newnode ();        Now=next[now][index]; } flag[now]=1; }voidBuild () { Queue<int>Q; Fail[root]=root; for(intI=0; i<len;i++) {if(next[root][i]==-1) Next[root][i]=root;Else{fail[next[root][i]]=root;          Q.push (Next[root][i]); }        } while(! Q.empty ()) {intNow=q.front ();if(Flag[fail[now]]) flag[now]++; Q.pop (); for(intI=0; i<len;i++) {if(next[now][i]==-1) Next[now][i]=next[fail[now]][i];Else{Fail[next[now][i]]=next[fail[now]][i];//flag[next[now][i]]|=flag[next[fail[now]][i]];Q.push (Next[now][i]); }            }        }    }}; Trie AC;CharSTR[MAXN]; BigInteger DP[MAXN][MAXN];intMain () {intN,m,p;///freopen ("In.txt", "R", stdin);One.Set(1); while(scanf("%d%d%d", &n,&m,&p)!=eof) {h.clear ();scanf('%s ', Alph); for(intI=0;i<strlen(Alph); i++) h[alph[i]]=i;        Ac.init (); ac.len=strlen(Alph); for(intI=0; i<p;i++) {scanf('%s ', str);        Ac.insert (str); } ac.build (); for(intI=0; i<=m;i++) for(intj=0; J<ac. l;j++) Dp[i][j].Set(0); dp[0][0].Set(1); for(intI=1; i<=m;i++) for(intj=0; J<ac. l;j++) for(intk=0; k<n;k++) {if(Ac.flag[ac.next[j][k]])Continue; dp[i][ac.next[j][k]]=dp[i][ac.next[j][k]]+dp[i-1][J];        } BigInteger ans; Ans.Set(0); for(intI=0; I<ac.        l;i++) Ans=ans+dp[m][i];    Ans.print (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Large number of dp+ on POJ--1625CENSORED!+AC automatic machine

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.