-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1030 [JSOI2007]文本生成器(AC自动机).cpp
94 lines (94 loc) · 2.09 KB
/
1030 [JSOI2007]文本生成器(AC自动机).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
f[i]表示已经生成了i的长度,且后缀是这样的情况数。
fail->f[i]+=f[i]
for c = each CH
if son[c]
son[c]->f[i+1]+=f[i]
else//fail
son[c]->f[i+1]+=f[i]
要自下而上做
*/
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const short P=10007;
#define inc(a,b) (a=(a+(b))%P)
int N,M;
template<char CH=26>
struct Trie{
Trie *son[CH],*fail; bool mark; short f[2];
Trie(){
memset(this,0,sizeof(Trie));
}
template<char NextChar(),bool EndChar(char)>
Trie* insert(){
char ch; Trie* p=this;
while(!EndChar(ch=NextChar())){
if(!p->son[ch]) p->son[ch]=new Trie;
p=p->son[ch];
}
p->mark=true;
return p;
}
void build(std::vector<Trie*> &q){
int head=0; Trie *x,*y; char ch;
fail=new Trie;
for(ch=0;ch<CH;ch++) fail->son[ch]=this;
for(ch=0;ch<CH;ch++) if(!son[ch]) son[ch]=this;
q.push_back(this);
while(head!=q.size())
for(x=q[head++],ch=0;ch<CH;++ch)
if((y=x->son[ch])&&y!=this){
q.push_back(y);
y->fail=x->fail->son[ch];
}
else
x->son[ch]=x->fail->son[ch];
for(int i=0;i<q.size();i++)
q[i]->mark|=q[i]->fail->mark;
}
};
typedef Trie<> trie;
#define get(c) ((c)-'A')
inline char nextc(){ return get(getchar()); }
inline bool endc(char c){ return c==get('\n'); }
trie words;
vector<trie*> ord;
typedef vector<trie*>::reverse_iterator trrit;
inline short pow(int a,int b){
int ret=1;
for(;b;b>>=1,a=a*a%P)
if(b&1) ret=ret*a%P;
return ret;
}
int main(){
scanf("%u%u\n",&N,&M);
for(int i=0;i<N;i++)
words.insert<nextc,endc>();
words.build(ord);
bool F=false;
words.f[F]=1;
for(int i=0;i<M;i++,F^=1){
for(trrit it=ord.rbegin();it!=ord.rend();it++)
(*it)->f[!F]=0;
for(trrit it=ord.rbegin();it!=ord.rend();it++){
trie *cur=*it;
if(cur->mark) continue;
//inc(cur->fail->f[F],cur->f[F]);
for(char ch=0;ch<26;ch++)
inc(cur->son[ch]->f[!F],cur->f[F]);
}
/*
for(trrit it=ord.rbegin();it!=ord.rend();it++)
printf("%6u",(*it)->mark?0:(*it)->f[!F]);
putchar('\n');
*/
}
short Ans=0;
for(trrit it=ord.rbegin();it!=ord.rend();it++)
if(!(*it)->mark){
inc(Ans,(*it)->f[F]);
}
printf("%hu\n",(pow(26,M)-Ans+P)%P);
}