zoj 1540 Censored! AC自动机+DP 长度为len的字符串不包含病毒串的个数

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.

Process to the end of file.


Output

Output the only integer number - the number of different sentences freelanders can safely use.


Sample Input

2 3 1
ab
bb
3 2 0
012
3 3 3
QWE
QQ
WEE
Q
2 50 4
AB
AA
AB
BA
BB


Sample Output

5
9
7
0


//


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=510;
struct Node
{
    int flag;//序号
    int id;//在静态链表中的位置
    Node* next[51];
    Node* fail;
};
Node temp[maxn];
int tp;
int n;
int len;
int kind;
int hash[200];
Node* root;
void reset(Node* p)
{
    p->flag=0;p->id=tp-1;
    for(int i=0;i<kind;i++) p->next[i]=NULL;
    p->fail=root;
    if(p==root) p->fail=NULL;
}
void init()
{
    tp=0;
    root=&temp[tp++];
    reset(root);
}
void insert(char* word)
{
    Node* p=root;
    for(int i=0;word[i];i++)
    {
        int x=hash[word[i]];
        if(p->next[x]==NULL)
        {
            p->next[x]=&temp[tp++];
            reset(p->next[x]);
        }
        p=p->next[x];
    }
    p->flag=1;
}
Node* que[maxn];
int front,rear;
void DFA()
{
    Node* p=root;
    front=rear=0;
    que[rear++]=p;
    while(front<rear)
    {
        Node* t=que[front++];
        for(int i=0;i<kind;i++)
        {
            Node* cnt=t->next[i];
            if(cnt!=NULL)
            {
                Node* fath=t->fail;
                while(fath!=NULL&&fath->next[i]==NULL)
                {
                    fath=fath->fail;
                }
                if(fath!=NULL)
                {
                    cnt->fail=fath->next[i];
                }
                else
                {
                    cnt->fail=p;
                }
                que[rear++]=cnt;
            }
        }
    }
}
void add(int *a,int *b)//a=a+b  从最低位到最高位 每一位存10000
{
int c[101];
int j=0;
for(int i=0;i<99;i++)
{
int k=a[i]+b[i]+j;
c[i]=k%10000;
j=k/10000;
}
for(int i=0;i<99;i++) a[i]=c[i];
}
void print(int *a)
{
int j=99;
while(j>=0&&a[j]==0) j--;
if(j==-1)
{
printf("0\n");
return;
}
printf("%d",a[j]);
  for(int i=j-1;i>=0;i--) printf("%04d",a[i]);
printf("\n");
}
int dp[51][501][100];
void toMatrix()
{
    Node* fath;
    memset(dp,0,sizeof(dp));
    for(int i=0;i<rear;i++)
    {
        Node* tmp=&temp[i];
        int mark=1;
        for(fath=tmp;fath!=NULL;fath=fath->fail)
        {
            if(fath->flag)
            {
                mark=0;
                break;
            }
        }
        if(mark) dp[0][i][0]=1;
    }
    for(int s=0;s<len;s++)//由于len比较小  故不用矩阵乘法
    {
        for(int i=0;i<rear;i++)
        {
            Node* p=&temp[i];
            if(p->flag) continue;
            for(int j=0;j<kind;j++)
            {
                Node* cnt=p->next[j];
                if(cnt!=NULL)
                {
                    int mark=1;
                    for(fath=cnt;fath!=NULL;fath=fath->fail)
                    {
                        if(fath->flag)
                        {
                            mark=0;
                            break;
                        }
                    }
                    if(mark)
                    {
                        int k=cnt->id;
                        add(dp[s+1][i],dp[s][k]);
                    }
                }
                else
                {
                    fath=p->fail;
                    while(fath!=NULL&&fath->next[j]==NULL)
                    {
                        fath=fath->fail;
                    }
                    if(fath!=NULL)
                    {
                        cnt=fath->next[j];
                        int mark=1;
                        for(fath=cnt;fath!=NULL;fath=fath->fail)
                        {
                            if(fath->flag)
                            {
                                mark=0;
                                break;
                            }
                        }
                        if(mark)
                        {
                            int k=cnt->id;
                            add(dp[s+1][i],dp[s][k]);
                        }
                    }
                    else
                    {
                        cnt=root;
                        add(dp[s+1][i],dp[s][0]);
                    }
                }
            }
        }
    }
}
int main()
{
    char str[60];
    while(scanf("%d%d%d",&kind,&len,&n)==3){
    scanf("%s",str);
    for(int i=0;str[i];i++) hash[str[i]]=i;
    init();
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        insert(str);
    }
    DFA();
    toMatrix();
    print(dp[len][0]);
    }
    return 0;
}


//上面代码在POJ上超内存  下面代码内存占用比较小 在POJ上能够过

#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdio>
using std::cin;
using std::cout;
using std::endl;
struct e{
int p[51];
int end;
}trie[501];


int n,m,K;
int hash[200];
int state=1;
int f[501];




void build(char c[]){
int i,j=0,k;
for(i=1;;)
{
if(trie[i].p[hash[c[j]]]==0)
trie[i].p[hash[c[j]]]=++state;
i=trie[i].p[hash[c[j]]];
j++;
if(j>=strlen(c))
break;
}
trie[i].end=1;
}


void ac(){
int q[501];
int l=0,r=0;
int i,j,k;
for(i=0;i<n;i++)
if(trie[1].p[i])
{
q[++r]=trie[1].p[i];
f[trie[1].p[i]]=1;
}
else
{
trie[1].p[i]=1;
}
while(l<r){
i=q[++l];


for(j=0;j<n;j++)
{
if(trie[i].p[j])
{
q[++r]=trie[i].p[j];
k=f[i];
while(!trie[k].p[j])
k=f[k];
f[trie[i].p[j]]=trie[k].p[j];
trie[trie[i].p[j]].end|=trie[trie[k].p[j]].end;
}
}
}
}


int dp[51][501][100];


void plus(int *a,int *b){
int i,j=0,k;
int c[101];
for(i=0;i<99;i++)
{
k=a[i]+b[i]+j;
c[i]=k%10000;
j=k/10000;
}
for(i=0;i<99;i++)
a[i]=c[i];
}


void print(int *a){
int i,j,k;
j=99;
while(j>=0&&a[j]==0)
j--;
if(j==-1)
{
cout<<0<<endl;
return;
}
cout<<a[j];cout.fill('0');
  for(i=j-1;i>=0;i--)
{
cout.width(4);
cout<<a[i];
}
cout<<endl;
}




void loop(){
    memset(dp,0,sizeof(dp));
int i,j,k,s;
for(i=1;i<=state;i++)
if(trie[i].end==0)
dp[0][i][0]=1;
for(s=0;s<m;s++)
for(i=1;i<=state;i++)
if(trie[i].end)
continue;
else
{
for(j=0;j<n;j++)
if(trie[i].p[j])
{
k=trie[i].p[j];
if(trie[k].end==0)
plus(dp[s+1][i],dp[s][k]);
}
else
{
k=f[i];
while(!trie[k].p[j])
k=f[k];
if(!trie[trie[k].p[j]].end)
{
k=trie[k].p[j];
plus(dp[s+1][i],dp[s][k]);
}
}
}
}






int main(){
int i,j,k;
char c[60];
while(cin>>n>>m>>K){
memset(trie,0,sizeof(trie));
state=1;
cin>>c;
for(i=0;i<strlen(c);i++)
hash[c[i]]=i;
for(i=1;i<=K;i++)
{
cin>>c;
build(c);
}
ac();
loop();
print(dp[m][1]);
}
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值