水题,上trie,然后穷举每一位的时候判定一下三种编辑

 1 var son:array[0..10010*20,1..26] of longint;
 2     v:array[0..10010*20] of longint;
 3     d:array[0..10010] of boolean;
 4     s:string;
 5     t,i,l,n,m:longint;
 6 
 7 procedure add(w:longint);
 8   var p,i,y:longint;
 9   begin
10     p:=1;
11     for i:=1 to length(s) do
12     begin
13       y:=ord(s[i])-96;
14       if son[p,y]=0 then
15       begin
16         inc(t);
17         son[p,y]:=t;
18       end;
19       p:=son[p,y];
20     end;
21     v[p]:=w;
22   end;
23 
24 function find(p,x:longint):longint;
25   var i,y:longint;
26   begin
27     if p=0 then exit(0);
28     for i:=x to l do
29     begin
30       y:=ord(s[i])-96;
31       if son[p,y]=0 then exit(0)
32       else p:=son[p,y];
33     end;
34     if (v[p]>0) and not d[v[p]] then
35     begin
36       d[v[p]]:=true;
37       exit(1)
38     end
39     else exit(0);
40   end;
41 
42 function ask:longint;
43   var p,i,y,j:longint;
44   begin
45     p:=1;
46     ask:=0;
47     for i:=1 to l do
48     begin
49       for j:=1 to 26 do
50         ask:=ask+find(son[p,j],i)+find(p,i+1)+find(son[p,j],i+1);
51                 // 在i前一位插入      删除      修改
52       y:=ord(s[i])-96;
53       p:=son[p,y];
54       if p=0 then exit;
55     end;
56     for i:=1 to 26 do
57       ask:=ask+find(son[p,i],l+1); //在最后一位插入
58   end;
59 
60 begin
61   readln(n,m);
62   t:=1;
63   for i:=1 to n do
64   begin
65     readln(s);
66     add(i);
67   end;
68   for i:=1 to m do
69   begin
70     fillchar(d,sizeof(d),false);
71     readln(s);
72     l:=length(s);
73     if find(1,1)>0 then writeln(-1)
74     else writeln(ask);
75   end;
76 end.
View Code

 

posted on 2015-01-26 17:59  acphile  阅读(185)  评论(0编辑  收藏  举报