Glob Matcher
You are writing the file-selection part of a small build tool. The tool knows m file paths, and
the user writes q glob patterns; for each pattern you must report which paths it matches. A
pattern matches a path only if it matches the whole path, not just a part of it.
All paths and patterns consist of printable ASCII characters with codes 33 to 126 (no spaces).
Pattern syntax
A pattern is a sequence of elements, read from left to right. Unless stated otherwise, an element is a single character that matches exactly itself. The special elements are:
\c(a backslash followed by any characterc) matches the single characterc. This is how special characters are written literally:\*matches*,\\matches\,\{matches{. A backslash before an ordinary character is allowed too:\amatchesa.?matches any single character.*matches any sequence of characters, including the empty one. The character/is not special:*and?match it like any other character.[starts a character class, which matches exactly one character. Its contents are read like this:- If the character right after
[is!, the class is negated: it matches any single character that is not in the set described by the rest of the class. In any other position,!is an ordinary character. - Then come one or more items, and the class ends at the first
]that is not the first item. So[]a]is the set {],a} and[!]]matches any character except]. - An item is read as follows: take the next character
x. If it is followed by-and the character after that-is not], the item is the rangex-y(whereyis the character after the-) meaning all characters with codes fromxtoyinclusive; it is guaranteed that the code ofxdoes not exceed the code ofy. Otherwise the item is the single characterx, and reading continues right afterx. In particular, a-that comes first or right before the closing]is an ordinary character:[-a]and[a-]are both the set {-,a}. - Inside a class, every character other than the closing
]and the leading!is ordinary. In particular, a backslash is a plain character there:[\]is a class containing only\, and*,?,[,{,},,have no special meaning inside a class either.
- If the character right after
{starts an alternation{b_1,b_2,...,b_k}(k ≥ 1), which matches whatever any one of its branches matches. Each branch is itself a pattern (possibly empty, possibly containing further alternations) that ends at an unescaped,or}that does not belong to a nested alternation or a class. An empty branch matches the empty string, soa{,b}matchesaandab, and{}matches only the empty string. Alternations are nested at most 4 levels deep.- Outside of every alternation, an unescaped
,is an ordinary character, as are],!and-everywhere outside of classes.
Every pattern is well formed: each [ that starts a class has its closing ], every { has a
matching }, an unescaped } never appears outside of an alternation, and a pattern never ends
with an unescaped \.
Input
The first line contains two integers m and q (1 ≤ m, q and m·q ≤ 2·10^4): the number of paths
and the number of patterns.
Each of the next m lines contains a path: a string of 1 to 100 printable ASCII characters with
codes 33 to 126. Paths are numbered from 1 to m in input order; they are not necessarily
distinct.
Each of the next q lines contains a pattern: a string of 1 to 100 characters with codes 33 to
126, well formed as described above, with alternations nested at most 4 levels deep and at most 6
* elements (a * that is escaped or inside a class does not count).
Output
For each pattern, in input order, print one line: first the number k of paths the pattern
matches, then the k indices of those paths in increasing order. If no path matches, print just
0.
Notes
In the first example, src/*.c matches src/main.c and also src/lib/util.c, because *
crosses /. The pattern {src,include}/*.{c,h} matches all four paths of the form src/….c or
include/….h, and [!s]* matches every path whose first character is not s.
In the second example, the pattern \{a\,b\}, matches the literal path {a,b},: escaped braces
and commas are ordinary characters, and so is an unescaped comma outside of any alternation. The
pattern []-]x matches ]x and -x, and [\]? matches any two-character path starting with a
backslash. The pattern a{,b{,c{,d}}} matches a, ab, abc and abcd. The pattern
{a,ab}{c,d} matches abc by taking the second branch of the first alternation, and {}x
matches x.