Grep with Flags
Bob keeps a log file and likes to search it with a tiny grep-like tool. Each search is a line of the form
grep <tokens...> <quoted pattern>
where <tokens...> is zero or more flags and the quoted pattern may appear anywhere among
the tokens, not necessarily last. The flags are:
-i— case-insensitive:A–Zanda-zare treated as equal when matching.-w— whole word: the match must not touch a word character on either side.-c— count only: report the number of matching lines instead of listing them.
Flags may repeat and appear in any order and in any position relative to the pattern (repeats have no extra effect). Exactly one token of each query is the quoted pattern.
A word character is any of A-Z, a-z, 0-9, or _. Every other character that can
appear in the log (space or punctuation) is a non-word character.
The quoted pattern
The pattern token starts with " and ends at the next unescaped ". Between them, the two-character
sequences \" and \\ represent a literal " and a literal \ respectively; every other character
represents itself (spaces included). The input is guaranteed well formed: inside the quotes every \
starts one of those two escape sequences, and there is no unescaped " before the closing one. The
decoded pattern (after resolving escapes) has length at least 1.
Tokens (flags and the quoted pattern) are separated by single spaces. A flag token is always exactly
one of -i, -w, -c.
Matching
Fix a query with decoded pattern p and its flags. A line matches if p occurs as a contiguous
substring of that line, compared letter-case-insensitively if -i is set and case-sensitively
otherwise. If -w is set, an occurrence only counts when the character immediately before it
(if the occurrence doesn't start at the beginning of the line) is a non-word character, and
the character immediately after it (if the occurrence doesn't end at the end of the line) is a
non-word character. Matches never cross line boundaries. A line matches the query if it has at
least one qualifying occurrence.
Input
The first line contains n (1 ≤ n ≤ 10^5) — the number of log lines.
Each of the next n lines contains one log line: a string of printable ASCII characters
(code points 0x20 to 0x7E), possibly empty. The total length of all log lines (not counting
line breaks) is at most 10^5.
The next line contains q (1 ≤ q ≤ 1500) — the number of queries.
Each of the next q lines contains one query in the format described above: the word grep,
a single space, then one or more tokens separated by single spaces. Every character used inside
a decoded pattern is a printable ASCII character (0x20 to 0x7E). The sum of the decoded pattern
lengths over all queries is at most 1500.
Output
For each query, in input order, print one line:
- If the query has the
-cflag, print a single integer: the number of matching log lines. - Otherwise, print
k, the number of matching log lines, followed by their 1-indexed line numbers in increasing order, all separated by spaces (just0ifkis 0).
Notes
In the first example, grep -w "cat" only matches lines where cat is not glued to another
word character, so concatenate on line 2 is excluded. grep -i "ERROR" matches both the
uppercase and lowercase spellings. grep -c "o" reports a count only, not the line numbers.