import re abc bca dac dbc cba ab bc ca abc abc abc abc zyx bc lines sp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import re
s = """3 5 4
abc
bca
dac
dbc
cba
(ab)(bc)(ca)
abc
(abc)(abc)(abc)
(zyx)bc"""
lines = s.split('\n')
L, D, N = lines[0].split()
words = lines[1:D]
words = lines[:int(D)]
patterns = lines[int(D):]
def r(s):
return s.replace("(", "[").replace(")", "]")
patterns = map(r, patterns)
patterns = map(re.compile, patterns)
def y(f):
def cl(y):
return 1 if f.match(y) else 0
return cl
def x(f):
return sum(map(y(f), words))
answ = map(x, patterns)