Time Limit: 5000MS | Memory Limit: 10000K | |
Total Submissions: 9793 | Accepted: 2686 |
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
题意:
给出p个模式串,求有多少个长度为m的字符串,其中不包含任何一个模式串为子串
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=,M=,L=,B=1e4;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,p,mp[];
char s[N];
struct node{
int ch[],fail,val;
}t[N];
int sz;
void ins(char s[]){
int u=,n=strlen(s+);
for(int i=;i<=n;i++){
int c=mp[s[i]];
if(!t[u].ch[c]) t[u].ch[c]=++sz;
u=t[u].ch[c];
}
t[u].val=;
}
int q[N],head,tail;
void getAC(){
head=tail=;
for(int i=;i<=mp[];i++) if(t[].ch[i]) q[tail++]=t[].ch[i];
while(head!=tail){
int u=q[head++];
t[u].val|=t[t[u].fail].val;
for(int i=;i<=mp[];i++){
int &v=t[u].ch[i];
if(!v) v=t[t[u].fail].ch[i];
else{
t[v].fail=t[t[u].fail].ch[i];
q[tail++]=v;
}
}
}
} struct big{
int size,d[L];
big():size(){memset(d,,sizeof(d));}
bool has(){return size>||(size==&&d[]!=);}
};
big operator +(big a,big b){
int g=,i;
for(i=;;i++){
if(g==&&i>a.size&&i>b.size) break;
int t=g;
t+=i<=a.size?a.d[i]:;
t+=i<=b.size?b.d[i]:;
a.d[i]=t%B;
g=t/B;
}
a.size=i-;
return a;
}
void print(big &a){
printf("%d",a.d[a.size]);
for(int i=a.size-;i>=;i--){
if(a.d[i]<) printf("");
else if(a.d[i]<) printf("");
else if(a.d[i]<) printf("");
printf("%d",a.d[i]);
}
putchar('\n');
} big f[M][N],ans;
void dp(){
f[][].d[]=;
for(int i=;i<m;i++)
for(int j=;j<=sz;j++) if(!t[j].val&&f[i][j].has())
for(int k=;k<=mp[];k++) if(!t[t[j].ch[k]].val)
f[i+][t[j].ch[k]]=f[i+][t[j].ch[k]]+f[i][j];
for(int i=;i<=sz;i++) ans=ans+f[m][i];
print(ans);
}
int main(){
freopen("in","r",stdin);
n=read();m=read();p=read();
scanf("%s",s+);
for(int i=;i<=n;i++) mp[s[i]]=i;mp[]=n;
for(int i=;i<=p;i++) scanf("%s",s+),ins(s);
//for(int i=1;i<=n;i++) printf("mp %c %d\n",s[i],mp[s[i]]);
getAC();
dp();
}