Glob Matcher

medium parsing backtracking strings dp 2000 ms · 256 MB

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 character c) matches the single character c. This is how special characters are written literally: \* matches *, \\ matches \, \{ matches {. A backslash before an ordinary character is allowed too: \a matches a.
  • ? 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 range x-y (where y is the character after the -) meaning all characters with codes from x to y inclusive; it is guaranteed that the code of x does not exceed the code of y. Otherwise the item is the single character x, and reading continues right after x. 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.
  • { 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, so a{,b} matches a and ab, 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.

Example 1

Input

6 7
src/main.c
src/lib/util.c
include/util.h
README.md
src/main.h
docs/index.html
src/*.c
{src,include}/*.{c,h}
[!s]*
*.??
[A-Z]*.md
*/*/*
docs/index.htm

Output

2 1 2
4 1 2 3 5
3 3 4 6
1 4
1 4
1 2
0

Example 2

Input

10 7
{a,b},
]x
-x
\x
a
ab
abc
abcd
abd
x
\{a\,b\},
[]-]x
[\]?
a{,b{,c{,d}}}
{a,ab}{c,d}
[!a-c]?
{}x

Output

1 1
2 2 3
1 4
4 5 6 7 8
2 7 9
3 2 3 4
1 10

Hints · reveal one at a time if you're stuck

Hint 1

Matching a pattern by backtracking can revisit the same state many times, such as the same pattern element at the same position in the path. What single pair of positions fully determines whether the rest of the pattern can still match?

Hint 2

Parse the pattern into a structure of elements first: literals, ?, *, classes and alternations, following the escape and class rules exactly (for example, a ] right after [ or [! is a member, and a - first or last is literal). Then decide matching recursively on (element, path position).

Hint 3

An alternation must try every branch, not stop at the first one that succeeds: after a branch ends, matching continues with whatever follows the whole group. Memoise each (element, position) result for the current path, using timestamps so the memo does not need clearing.

report an issue Prefer your own editor? sooks prep 14 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.