Language:
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
|
题意:给定n个不同的字符和p个非法串,求长度为m的不包含非法串的序列个数。
思路:将非法串建立ac机,设dp[i][j]为长度为i,到达ac机上的j节点位置的合法方案数。
设j->next[k]->id=next,那么状态转移方程dp[i+1][next]+=dp[i][j],保证j,next节点位置均合法。有两个注意点:
(1)需要用gets读取字符串,因为可能出现空格。
(2)题目中没有取模,那么答案需要高精度。
详见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int MAXN=10000+10;
const int N=100;
const int sigma_size=128;
int n,m,p,head,tail,sz;
char s[MAXN];
map<char,int>Q;
struct BigInt
{
const static int mod=10000;
const static int Dlen=4;
int a[150],len;
BigInt()
{
memset(a,0,sizeof(a));
len=1;
}
BigInt(int x)
{
memset(a,0,sizeof(a));
len=0;
do
{
a[len++]=x%mod;
x/=mod;
}while(x);
}
BigInt(const char *str)
{
memset(a,0,sizeof(a));
int l=strlen(str),index=0;
len=(l+Dlen-1)/Dlen;
for(int i=l-1;i>=0;i-=Dlen)
{
int x=0;
int k=i-Dlen+1;
if(k<0) k=0;
for(int j=k;j<=i;j++)
x=x*10+(str[j]-'0');
a[index++]=x;
}
}
BigInt operator +(const BigInt &rhs) const
{
BigInt res;
res.len=max(len,rhs.len);
for(int i=0;i<res.len;i++)
{
res.a[i]+=((i<len)?a[i]:0)+((i<rhs.len)?rhs.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 &rhs) const
{
BigInt res;
for(int i=0;i<len;i++)
{
int add=0;
for(int j=0;j<rhs.len;j++)
{
int x=a[i]*rhs.a[j]+res.a[i+j]+add;
res.a[i+j]=x%mod;
add=x/mod;
}
if(add)
res.a[i+rhs.len]+=add;
}
res.len=len+rhs.len;
while(!res.a[res.len-1] && res.len>=0) 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");
}
}dp[N][N];
struct node
{
int cnt,id;
node *next[sigma_size],*fail;
}trie[MAXN],*root,*que[MAXN];
struct AC
{
node *createnode()
{
for(int k=0;k<n;k++)
trie[sz].next[k]=NULL;
trie[sz].cnt=0; trie[sz].id=sz;
return &trie[sz++];
}
void init()
{
sz=0;
head=tail=0;
root=createnode();
}
void insert(char *str)
{
int len=strlen(str);
node *p=root;
for(int i=0;i<len;i++)
{
int k=Q[str[i]];
if(p->next[k]==NULL)
p->next[k]=createnode();
p=p->next[k];
}
p->cnt=1;
}
void get_fail()
{
que[tail++]=root;
while(head<tail)
{
node *p=que[head++];
for(int k=0;k<n;k++)
if(p->next[k])
{
if(p==root)
p->next[k]->fail=root;
else
p->next[k]->fail=p->fail->next[k];
if(p->next[k]->fail->cnt)
p->next[k]->cnt=1;
que[tail++]=p->next[k];
}
else
{
if(p==root)
p->next[k]=root;
else
p->next[k]=p->fail->next[k];
}
}
}
void query(int m)
{
for(int i=0;i<=m;i++)
for(int j=0;j<sz;j++)
dp[i][j]=BigInt(0);
dp[0][0]=BigInt(1);
for(int i=0;i<m;i++)
for(int j=0;j<sz;j++)
{
if(trie[j].cnt)
continue;
for(int k=0;k<n;k++)
{
int p=trie[j].next[k]->id;
if(trie[p].cnt)
continue;
dp[i+1][p]=dp[i+1][p]+dp[i][j];
}
}
BigInt ans=BigInt(0);
for(int i=0;i<sz;i++)
ans=ans+dp[m][i];
ans.output();
}
}ac;
int main()
{
freopen("text.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&p))
{
ac.init(); Q.clear();
getchar();
gets(s);
for(int i=0;i<n;i++)
Q[s[i]]=i;
for(int i=0;i<p;i++)
{
gets(s);
ac.insert(s);
}
ac.get_fail();
ac.query(m);
}
return 0;
}