problems / 101

Grep with Flags

easy parsing string-matching strings 2000 ms · 256 MB

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–Z and a-z are 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 -c flag, 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 (just 0 if k is 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.

Example 1

Input

5
the cat sat
concatenate it
ERROR: timeout
error: retry now
all good
3
grep -w "cat"
grep -i "ERROR"
grep -c "o"

Output

1 1
2 3 4
4

Example 2

Input

5
error: x"y occurred
time_out now
TIME OUT NOW
a\b marker
noise
5
grep -c "x\"y"
grep -i -w "time out"
grep "marker" -w
grep -i -i -w -w "NOISE"
grep -c "a\\b"

Output

1
1 3
1 4
1 5
1

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

Hint 1

Parsing comes first: the quoted pattern can sit anywhere among the flags, and inside the quotes a backslash escapes the next character. Walk each query once, left to right, and decode the pattern as you meet it.

Hint 2

Matching is a substring search with two twists: fold case only when -i is set, and with -w check the characters on both sides of every occurrence, not just the first one. A glued early occurrence does not rule out a later whole-word one, and -c counts lines rather than occurrences.

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

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