Time Limit: 5000MS | Memory Limit: 10000K | |
Total Submissions: 6956 | Accepted: 1887 |
Description
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 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
Sample Input
2 3 1
ab
bb
Sample Output
5
Source
//============================================================================
// Name : POJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <map>
using namespace std;
map<char,int>mp;
int N,M,P;
struct Matrix
{
int mat[][];
int n;
Matrix(){}
Matrix(int _n)
{
n=_n;
for(int i = ;i < n;i++)
for(int j = ;j < n;j++)
mat[i][j] = ;
}
};
struct Trie
{
int next[][],fail[];
bool end[];
int L,root;
int newnode()
{
for(int i = ;i < ;i++)
next[L][i] = -;
end[L++] = false;
return L-;
}
void init()
{
L = ;
root = newnode();
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = ;i < len;i++)
{
if(next[now][mp[buf[i]]] == -)
next[now][mp[buf[i]]] = newnode();
now = next[now][mp[buf[i]]];
}
end[now] = true;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ;i < ;i++)
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
if(end[fail[now]]==true)end[now]=true;
for(int i = ;i < ;i++)
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
Matrix getMatrix()
{
Matrix res = Matrix(L);
for(int i = ;i < L;i++)
for(int j = ;j < N;j++)
if(end[next[i][j]]==false)
res.mat[i][next[i][j]]++;
return res;
}
void debug()
{
for(int i = ;i < L;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j = ;j < ;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
} }; /*
* 高精度,支持乘法和加法
*/
struct BigInt
{
const static int mod = ;
const static int DLEN = ;
int a[],len;
BigInt()
{
memset(a,,sizeof(a));
len = ;
}
BigInt(int v)
{
memset(a,,sizeof(a));
len = ;
do
{
a[len++] = v%mod;
v /= mod;
}while(v);
}
BigInt(const char s[])
{
memset(a,,sizeof(a));
int L = strlen(s);
len = L/DLEN;
if(L%DLEN)len++;
int index = ;
for(int i = L-;i >= ;i -= DLEN)
{
int t = ;
int k = i - DLEN + ;
if(k < )k = ;
for(int j = k;j <= i;j++)
t = t* + s[j] - '';
a[index++] = t;
}
}
BigInt operator +(const BigInt &b)const
{
BigInt res;
res.len = max(len,b.len);
for(int i = ;i <= res.len;i++)
res.a[i] = ;
for(int i = ;i < res.len;i++)
{
res.a[i] += ((i < len)?a[i]:)+((i < b.len)?b.a[i]:);
res.a[i+] += res.a[i]/mod;
res.a[i] %= mod;
}
if(res.a[res.len] > )res.len++;
return res;
}
BigInt operator *(const BigInt &b)const
{
BigInt res;
for(int i = ; i < len;i++)
{
int up = ;
for(int j = ;j < b.len;j++)
{
int temp = a[i]*b.a[j] + res.a[i+j] + up;
res.a[i+j] = temp%mod;
up = temp/mod;
}
if(up != )
res.a[i + b.len] = up;
}
res.len = len + b.len;
while(res.a[res.len - ] == &&res.len > )res.len--;
return res;
}
void output()
{
printf("%d",a[len-]);
for(int i = len-;i >= ;i--)
printf("%04d",a[i]);
printf("\n");
}
};
char buf[];
BigInt dp[][];
Trie ac;
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout); while(scanf("%d%d%d",&N,&M,&P)==)
{
gets(buf);
gets(buf);
mp.clear();
int len = strlen(buf);
for(int i = ;i < len;i++)
mp[buf[i]]=i;
ac.init();
for(int i = ;i < P;i++)
{
gets(buf);
ac.insert(buf);
}
ac.build();
// ac.debug();
Matrix a= ac.getMatrix(); // for(int i = 0;i <a.n;i++)
// {
// for(int j=0;j<a.n;j++)printf("%d ",a.mat[i][j]);
// cout<<endl;
// } int now = ;
dp[now][] = ;
for(int i = ;i < a.n;i++)
dp[now][i] = ;
for(int i = ;i < M;i++)
{
now^=;
for(int j = ;j < a.n;j++)
dp[now][j] = ;
for(int j = ;j < a.n;j++)
for(int k = ;k < a.n;k++)
if(a.mat[j][k] > )
dp[now][k] = dp[now][k]+dp[now^][j]*a.mat[j][k];
// for(int j = 0;j < a.n;j++)
// dp[now][j].output();
}
BigInt ans = ;
for(int i = ;i < a.n;i++)
ans = ans + dp[now][i];
ans.output();
}
return ;
}