POJ1625---censored! (AC automaton +dp+ high accuracy)

Source: Internet
Author: User

Description
The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly n^m different freish sentences.

But after recent election of Mr. Grass Jr. As Freeland president some words offending him were declared unprintable and al L sentences containing at least one of them were forbidden. The sentence S contains a word w if W is a substring of S i.e. exists such K >= 1 that s[k] = w[1], s[k+1] = w[2], ..., s[ K+len (W)-1] = W[len (w)], where K+len (w)-1 <= M and Len (w) denotes length of W. Everyone who uses a forbidden sentence I s to is put to jail for ten years.

Find out how many different sentences can is used now by freelanders without risk to being put to jail for using it.

Input
The first line of the input file contains three integer numbers:n–the number of letters in Freish alphabet, M–the len Gth of all freish sentences and p–the number of forbidden words (1 <= N <=, 1 <= M <=, 0 <= P <= 10).

The second line contains exactly N different characters–the letters of the Freish alphabet (all with ASCII code greater than 32).

The following P lines contain forbidden words, each not longer than min (M, ten) characters, all containing only letters of Freish alphabet.

Output
Output the only integer number–the number of different sentences freelanders can safely use.

Sample Input

2 3 1
Ab
Bb

Sample Output

5

Source
Northeastern Europe 2001, Northern subregion

AC Automatic Machine +DP
DP[I][J] Represents the length of I, the number of scenarios in which the node J satisfies the condition
To use the high precision
Attention to the character discretization, due to the existence of ASCLL in 128~255, converted to int is negative, here to note

/************************************************************************* > File Name:POJ1625.cpp > Author:alex > Mail: [email protected] > Created time:2015 March 09 Monday 15:08 58 Seconds ********************************* ***************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;structbigint{Const Static intMoD =10000;Const Static intDlen =4;inta[ -], Len;voidClear () {memsetA0,sizeof(a)); } BigInt () {memsetA0,sizeof(a)); } BigInt (intV) {memsetA0,sizeof(a)); Len =0; Do{a[len++] = v mod;        v/= MoD; } while(v); } BigIntoperator+ (ConstBigInt &b)Const{BigInt res; Res.len = max (len, B.len); for(inti =0; i < Res.len; ++i) {Res.a[i] =0; } for(inti =0; i < Res.len; ++i) {Res.a[i] + = ((i < len) A[i]:0) + ((I < B.len) B.a[i]:0); Res.a[i +1] + = res.a[i]/mod;        Res.a[i]%= mod; }if(Res.a[res.len] >0) {++res.len; }returnRes } bigint&operator= (ConstBigInt &b) { Thislen = B.len; for(inti =0; i < B.len; ++i) { ThisA[i] = B.a[i]; }return* This; } BigIntoperator* (ConstBigInt &b)Const{BigInt res; for(inti =0; i < Len; ++i) {intup =0; for(intj =0; J < B.len; ++J) {intTMP = a[i] * B.a[j] + res.a[i + j] + up;                Res.a[i + j] = tmp% mod;            up = Tmp/mod; }if(Up! =0) {res.a[i + B.len] = up; }} Res.len = Len + B.len; while(Res.a[res.len-1] ==0&& Res.len >1) {--res.len; }returnRes }voidOutput () {printf("%d", A[len-1]); for(inti = len-2; I >=0; -I.) {printf("%04D", A[i]); }printf("\ n"); }};Const intMax_node = the; BigInt dp[2][max_node];intChild_num; Map <char, int>HASH;BOOL operator= = (BigInt &c, BigInt &d) {if(C.len! = D.len) {return 0; } for(inti =0; i < C.len; ++i) {if(C.a[i]! = D.a[i]) {return 0; }    }return 1;}structac_automation{intnext[max_node][ -];intFail[max_node];BOOLEnd[max_node];intRoot, L;intID (Charc) {returnHASH[C]; }intNewNode () { for(inti =0; i < Child_num; ++i) {Next[l][i] =-1; } end[l++] =0;returnL1; }voidInit () {L =0;    root = NewNode (); }voidBuild_trie (CharBuf[]) {intnow = root;intLen =strlen(BUF); for(inti =0; i < Len; ++i) {if(Next[now][id (buf[i]) = =-1) {Next[now][id (buf[i])] = NewNode ();        } now = Next[now][id (Buf[i]); } End[now] =1; }voidBuild_ac () { Queue <int>Qu Fail[root] = root; for(inti =0; i < Child_num; ++i) {if(Next[root][i] = =-1) {Next[root][i] = root; }Else{Fail[next[root][i]] = root;            Qu.push (Next[root][i]); }        } while(!qu.empty ()) {intnow = Qu.front (); Qu.pop ();if(End[fail[now]]) {End[now] =1; } for(inti =0; i < Child_num; ++i) {if(Next[now][i] = =-1) {Next[now][i] = Next[fail[now]][i]; }Else{Fail[next[now][i]] = next[fail[now]][i];                Qu.push (Next[now][i]); }            }        }    }voidSolve (intN) {BigInt x (1);        BigInt y; Y.clear (); for(inti =0; I <=1; ++i) { for(intj =0; J < L;            ++J) {dp[i][j].clear (); }} dp[0][0] = x; for(inti =1; I <= N; ++i) { for(intj =0; J < L; ++J) {Dp[i%2][j].clear (); } for(intj =0; J < L; ++J) { for(intK =0; K < Child_num; ++K) {intNXT = Next[j][k];if(END[NXT]) {Continue; } dp[i%2][NXT] = (dp[i%2][NXT] + dp[1I2][J]); }}} BigInt ans (0); for(inti =0; i < L; ++i) {if(End[i]) {Continue; } ans = (ans + dp[n%2][i]);    } ans.output (); }}ac;Charbuf[ -];Charalp[ -];intMain () {intM, p; while(~scanf("%d%d%d", &child_num, &m, &p)) {ac.init ();        Gets (ALP); Gets (ALP); for(inti =0; i < Child_num;        ++i) {Hash[alp[i]] = i; } for(inti =1; I <= p;            ++i) {gets (BUF); AC.        Build_trie (BUF); } AC.        Build_ac ();    Ac.solve (m); }return 0;}

POJ1625---censored! (AC automaton +dp+ high accuracy)

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.