POJ1625---Censored!(AC自動機+dp+高精度),ac自動機
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自動機+dp
dp[i][j]表示長度為i,在節點j滿足條件的方案數
要用到高精度
注意給字元離散化的時候,由於存在ascll在128~255的,轉換為int的時候為負,這裡要注意
/************************************************************************* > File Name: POJ1625.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年03月09日 星期一 15時08分58秒 ************************************************************************/#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 double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long 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;char buf[20];char alp[55];int main (){ int m, p; while (~scanf("%d%d%d", &CHILD_NUM, &m, &p)) { AC.init(); gets(alp); gets(alp); for (int i = 0; i < CHILD_NUM; ++i) { HASH[alp[i]] = i; } for (int i = 1; i <= p; ++i) { gets(buf); AC.Build_Trie(buf); } AC.Build_AC(); AC.solve(m); } return 0;}