[bzoj1030]文本生成器

 1 var i,j,k,n,m,len,head,tail,p,tot,ans1,ans2,x:Longint;
 2     ch:array[0..20000] of array[0..30] of Longint;
 3     fail:array[0..20000] of Longint;
 4     fg:array[0..20000] of Boolean;
 5     q:array[0..20000] of Longint;
 6     f:array[0..200] of array[0..20000] of Longint;
 7     str:string;
 8 
 9 begin
10     fillchar(ch,sizeof(ch),0);
11     fillchar(q,sizeof(q),0);
12     fillchar(f,sizeof(f),0);
13     tot:=0;
14     head:=0;
15     tail:=1;
16     fail[0]:=-1;
17     q[tail]:=0;
18     ans1:=1;
19     for i:=0 to 15000 do fg[i]:=false;
20     readln(n,m);
21     for i:=1 to n do
22     begin
23         readln(str);
24         len:=length(str);
25         p:=0;
26         for j:=1 to len do
27         begin
28             if ch[p][ord(str[j])-ord('A')]=0 then
29             begin
30                 inc(tot);
31                 ch[p][ord(str[j])-ord('A')]:=tot;
32             end;
33             p:=ch[p][ord(str[j])-ord('A')];
34         end;
35         fg[p]:=true;
36     end;
37     
38     while head<>tail do
39     begin
40         inc(head);
41         x:=q[head];
42         for i:=0 to 25 do
43         begin
44             if ch[x][i]<>0 then
45             begin
46                 inc(tail);
47                 q[tail]:=ch[x][i];
48                 if x<>0 then
49                     fail[ch[x][i]]:=ch[fail[x]][i]
50                 else fail[ch[x][i]]:=0;
51             end
52             else begin
53                 if x<>0 then
54                     ch[x][i]:=ch[fail[x]][i]
55                 else ch[x][i]:=0;
56             end;
57         end;
58         fg[x]:=fg[x] or fg[fail[x]];
59     end;
60     f[0][0]:=1;
61     for i:=1 to m do
62         for j:=0 to tot do
63             if not fg[j] then
64                 for k:=0 to 25 do
65                     f[i][ch[j][k]]:=(f[i-1][j]+f[i][ch[j][k]]) mod 10007;
66     for i:=0 to tot do
67         if not fg[i] then
68             inc(ans2,f[m][i]);
69     for i:=1 to m do ans1:=(ans1*26) mod 10007;
70     writeln((((ans1-ans2) mod 10007)+10007) mod 10007);
71 end.
View Code

 

转载于:https://www.cnblogs.com/KingSann/articles/6358983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值