POJ1625 --- Censored! (AC automation + dp + high precision) and ac Automation
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 all 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 is to be put to jail for 10 years.
Find out how many different sentences can be used now by freelanders without risk to be 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 length of all Freish sentences and P-the number of forbidden words (1 <= N <= 50, 1 <= M <= 50, 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, 10) 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 automation + dp
Dp [I] [j] indicates the number of solutions where node j meets the conditions.
High Precision
Note that the ascll value ranges from 128 to during discretization ~ 255, which is negative when converted to int. Note that
/*************************************** * *********************************> File Name: POJ1625.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: monday, ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstri Ng> # include <iostream> # include <algorithm> using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; struct BigInt {const static int mod = 10000; const static int DLEN = 4; int a [600], len; void clear () {memset (a, 0, sizeof (a);} BigInt () {memset (a, 0, sizeof (a);} BigInt (int v) {memset (a, 0, sizeof (A); len = 0; do {a [len ++] = v % mod; v/= mod;} while (v );} bigInt operator + (const BigInt & B) const {BigInt res; res. len = max (len, B. len); for (int I = 0; I <res. len; ++ I) {res. a [I] = 0 ;}for (int I = 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;} return res;} BigInt & operator = (const BigInt & B) {this-> len = B. len; for (int I = 0; I <B. len; ++ I) {this-> a [I] = B. a [I];} return * this;} BigInt operator * (const BigInt & B) const {BigInt res; for (int I = 0; I <len; ++ I) {int up = 0; for (int j = 0; j <B. len; ++ j) {int tmp = 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;} return res;} void output () {printf ("% d", a [len-1]); for (int I = len-2; I >= 0; -- I) {printf ("% 04d", a [I]);} printf ("\ n") ;}; const int MAX_NODE = 110; bigInt dp [2] [MAX_NODE]; int CHILD_NUM; map <char, int> HASH; bool operator = (BigInt & c, BigInt & d) {if (c. len! = D. len) {return 0 ;}for (int I = 0; I <c. len; ++ I) {if (c. a [I]! = D. a [I]) {return 0 ;}} return 1 ;}struct AC_Automation {int next [MAX_NODE] [55]; int fail [MAX_NODE]; bool end [MAX_NODE]; int root, L; int ID (char c) {return HASH [c];} int newnode () {for (int I = 0; I <CHILD_NUM; ++ I) {next [L] [I] =-1;} end [L ++] = 0; return L-1;} void init () {L = 0; root = newnode ();} void Build_Trie (char buf []) {int now = root; int len = strlen (buf); for (int I = 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;} void Build_AC () {queue <int> qu; fail [root] = root; for (int I = 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 () {int now = qu. front (); qu. pop (); if (end [fail [now]) {end [now] = 1 ;}for (int I = 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]) ;}}} void solve (int n) {BigInt x (1); BigInt y; y. clear (); for (int I = 0; I <= 1; ++ I) {for (int j = 0; j <L; ++ j) {dp [I] [j]. clear ();}} Dp [0] [0] = x; for (int I = 1; I <= n; ++ I) {for (int j = 0; j <L; ++ j) {dp [I % 2] [j]. clear () ;}for (int j = 0; j <L; ++ j) {for (int k = 0; k <CHILD_NUM; ++ k) {int nxt = next [j] [k]; if (end [nxt]) {continue ;} dp [I % 2] [nxt] = (dp [I % 2] [nxt] + dp [1-I % 2] [j]) ;}} bigInt ans (0); for (int I = 0; I <L; ++ I) {if (end [I]) {continue ;} ans = (ans + dp [n % 2] [I]);} ans. output () ;}} AC; ch Ar buf [20]; char phosphatase [55]; int main () {int m, p; while (~ Scanf ("% d", & CHILD_NUM, & m, & p) {AC. init (); gets (alkaline); for (int I = 0; I <CHILD_NUM; ++ I) {HASH [phosphatase [I] = I ;}for (int I = 1; I <= p; ++ I) {gets (buf); AC. build_Trie (buf);} AC. build_AC (); AC. solve (m);} return 0 ;}