2013
11-10

# Censored!

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.

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 the only integer number — the number of different sentences freelanders can safely use.

2 3 1
ab
bb


5


import java.math.*;
import java.util.*;
import java.io.*;
import java.util.*;

public class Main {
int M = 110;
int CD;
int fail[] = new int[M];
int Q[] = new int[M];
int ch[][] = new int[M][55];
int ID[] = new int[256];
int sz;
int flag[] = new int[M];
String DIC;
BigInteger dp[][] = new BigInteger[55][M];
BigInteger ans;
PrintStream out = System.out;
int m;

public void Init() {
fail[0] = 0;
flag[0] = 0;
Arrays.fill(ch[0], 0);
sz = 1;
for (int i = 0; i < DIC.length(); i++)
ID[DIC.charAt(i)] = i;
CD = DIC.length();
}

public void Insert(String s) {
int p = 0;
for (int i = 0; i < s.length(); i++) {
int c = ID[s.charAt(i)];
if (ch[p][c] == 0) {
Arrays.fill(ch[sz], 0);
flag[sz] = 0;
ch[p][c] = sz++;
}
p = ch[p][c];
}
flag[p] = 1;
}

void Construct() {
int head = 0, tail = 0;
for (int i = 0; i < CD; i++) {
if (ch[0][i] != 0) {
fail[ch[0][i]] = 0;
Q[tail++] = ch[0][i];
}
}
for (int i = 0; i < CD; i++) {
int v = ch[u][i];
if (v != 0) {
Q[tail++] = v;
fail[v] = ch[fail[u]][i];
flag[v] += flag[fail[v]];
} else {
ch[u][i] = ch[fail[u]][i];
}
}
}
}
public void DP(){
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= sz; j++) {
dp[i][j] = BigInteger.ZERO;
}
}
dp[0][0] = BigInteger.ONE;
for (int i = 0; i < m; i++) {
for (int j = 0; j < sz; j++)
if (dp[i][j].compareTo(BigInteger.valueOf(0)) == 1) {
for (int k = 0; k < CD; k++) {
if (flag[ch[j][k]] > 0 || flag[j] > 0)
continue;
dp[i + 1][ch[j][k]] = dp[i + 1][ch[j][k]].add(dp[i][j]);
}
}
}
ans = BigInteger.ZERO;
for (int i = 0; i < sz; i++)
out.println(ans);
}
public void solve() {
int n, p ;
try {
n = Integer.parseInt(args[0]);
m = Integer.parseInt(args[1]);
p = Integer.parseInt(args[2]);
Init();
for (int i = 0; i < p; i++) {
Insert(s);
}
Construct();
DP();
} catch(IOException e){
}
}

public static void main(String args[]) {
new Main().solve();
}
}

1. 第一题是不是可以这样想，生了n孩子的家庭等价于n个家庭各生了一个1个孩子，这样最后男女的比例还是1:1