字符串模拟+快排 program P1228; var n,q:longint; s:array ['a'..'z',1..1000] of ansistring; t:array ['a'..'z'] of longint;//用了一个类似于邻接表性质的东西吧,t用来记录以每个字母为首的单词有多少个 a:array [-1..10000] of ansistring; d:array [-1..10000] of ansistring; procedure init; var i:longint; begin readln(n); for i:=1 to n do readln(a[i]); end; procedure qsort(l,r:longint); var i,j:longint; m,temp:ansistring; begin i:=l; j:=r; m:=a[(l+r) shr 1]; repeat while a[i]<m do inc(i); while a[j]>m do dec(j); if i<=j then begin temp:=a[i]; a[i]:=a[j]; a[j]:=temp; inc(i); dec(j); end; until i>j; if i<r then qsort(i,r); if l<j then qsort(l,j); end; procedure main; var i:longint; begin for i:=1 to n do begin inc(t[a[i,1]]); s[a[i,1],t[a[i,1]]]:=a[i]; end;//类似邻接表的东西,把排好序的字符串记录一下 end; procedure outit; var i,j,tot:longint; begin readln(q); for i:=1 to q do begin tot:=0; readln(d[i]); if t[d[i,1]]=0 then writeln(d[i])//如果没有这个单词就直接输出 else begin for j:=1 to t[d[i,1]] do begin if (pos(d[i],s[d[i,1],j])<>0) and (s[d[i,1],j]<>s[d[i,1],j+1]) and (pos(d[i],s[d[i,1],j])=1) then//就这卡了半天啊!!!d[i]不仅在这个字串里面,还得在这个字串的开头,并且还不能出现相同的字串啊 begin inc(tot); if tot=9 then break;//超过8个结束循环 if tot<>8 then write(s[d[i,1],j],' ') else if tot=8 then write(s[d[i,1],j]); end; end; if tot=0 then write(d[i]);//虽然有以d[i,1]为首的单词但是里面没有以它为前缀的单词,那么直接输出d[i] writeln; end; end; end; begin assign(input,'P1228.in'); reset(input); assign(output,'P1228.out'); rewrite(output); init; qsort(1,n); main; outit; close(input); close(output); end.