1 /**************************************************************
2 Problem: 3297
3 User: HansBug
4 Language: Pascal
5 Result: Accepted
6 Time:1204 ms
7 Memory:2832 kb
8 ****************************************************************/
9
10 var
11 i,j,k,l,m,n:longint;
12 a,b:array[0..2000] of ansistring;
13 s1:ansistring;
14 function check(x:longint;s2:ansistring):boolean;
15 var i:longint;
16 begin
17 for i:=1 to length(s2) do
18 if (s1[x+i]<>‘?‘) and (s1[x+i]<>s2[i]) then exit(false);
19 exit(true);
20 end;
21 begin
22 readln(m,n);
23 readln(s1);
24 for i:=1 to n do readln(a[i]);
25 for i:=1 to m do
26 for j:=1 to n do
27 begin
28 k:=i-length(a[j]);
29 if (k<0) or ((k<>0) and (b[k]=‘‘)) then continue;
30 if check(k,a[j]) then
31 begin
32 if (b[i]=‘‘) or (b[i]>(b[k]+a[j])) then b[i]:=b[k]+a[j];
33 k:=0;
34 end;
35 end;
36 writeln(b[m]);
37 readln;
38 end.