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

 var son:array[..*,..] of longint;
v:array[..*] of longint;
d:array[..] of boolean;
s:string;
t,i,l,n,m:longint; procedure add(w:longint);
var p,i,y:longint;
begin
p:=;
for i:= to length(s) do
begin
y:=ord(s[i])-;
if son[p,y]= then
begin
inc(t);
son[p,y]:=t;
end;
p:=son[p,y];
end;
v[p]:=w;
end; function find(p,x:longint):longint;
var i,y:longint;
begin
if p= then exit();
for i:=x to l do
begin
y:=ord(s[i])-;
if son[p,y]= then exit()
else p:=son[p,y];
end;
if (v[p]>) and not d[v[p]] then
begin
d[v[p]]:=true;
exit()
end
else exit();
end; function ask:longint;
var p,i,y,j:longint;
begin
p:=;
ask:=;
for i:= to l do
begin
for j:= to do
ask:=ask+find(son[p,j],i)+find(p,i+)+find(son[p,j],i+);
// 在i前一位插入 删除 修改
y:=ord(s[i])-;
p:=son[p,y];
if p= then exit;
end;
for i:= to do
ask:=ask+find(son[p,i],l+); //在最后一位插入
end; begin
readln(n,m);
t:=;
for i:= to n do
begin
readln(s);
add(i);
end;
for i:= to m do
begin
fillchar(d,sizeof(d),false);
readln(s);
l:=length(s);
if find(,)> then writeln(-)
else writeln(ask);
end;
end.
05-08 15:51