Content Filter Rulebook
A moderation service keeps its banned phrases in a plain-text rulebook. Every rule is a byte string written as a double-quoted literal with C-style escapes, and the file may also contain comments and blank lines. Before rolling out a new rulebook, the moderators want to know how hard it would fire on a sample document: for every rule, how many times its byte string occurs in the document, and the grand total over all rules.
Rulebook syntax
The rulebook is a sequence of lines. Every character of a line has code 9 (tab) or a code from 32 to 126 (printable ASCII, including space); the line break itself is not part of the line. A blank is a space or a tab. Each line has the following form, read from left to right:
- any number (possibly zero) of blanks;
- optionally, one literal, followed by any number of blanks;
- optionally, a comment: the character
#followed by anything up to the end of the line.
So a line can be empty, consist of blanks only, hold only a comment, hold only a literal, or hold
a literal followed by a comment. A # that appears inside a literal is part of the literal and
does not start a comment; everything after a # that starts a comment is ignored, including any
" characters. Lines never contain anything else, and there is at most one literal per line.
A literal starts with " and ends at the next " that is not part of an escape sequence. Between
the quotes it consists of items, each of which stands for exactly one byte:
- any character with a code from 32 to 126 other than
"and\stands for itself (in particular, spaces and#inside a literal are ordinary characters); \\stands for a backslash (code 92);\"stands for a double quote (code 34);\nstands for a line feed (code 10);\tstands for a tab (code 9);\xHH, whereHHis exactly two hexadecimal digits (0–9,a–f,A–F, in any mix of cases), stands for the byte with that value, from 0 to 255.
No other use of \ occurs inside a literal, and a raw tab never occurs inside a literal. Every
literal contains at least one item. The byte string a literal stands for is the pattern of
that rule. Rules are numbered from 1 in the order their literals appear in the rulebook. Several
rules may have the same pattern, even if their literals are spelled differently (for example
"a" and "\x61"); they are still separate rules.
The document
The document is a string of T bytes. Each byte is a tab (9), a line feed (10), or has a code from
32 to 126. A rule occurs at position i (1 ≤ i ≤ T) if the pattern is exactly equal to the
bytes of the document starting at position i. Occurrences may overlap: the pattern aa occurs 3
times in aaaa.
Input
The first line contains an integer L (1 ≤ L ≤ 2·10^5): the number of lines in the rulebook.
The next L lines are the rulebook, each line terminated by a line feed (code 10). The total size
of these L lines, line feeds included, is at most 5·10^6 bytes. The rulebook contains k rules,
where 1 ≤ k ≤ 10^4, and the total length of all k patterns (in bytes, after decoding escapes) is
at most 10^6.
The next line contains an integer T (1 ≤ T ≤ 10^6): the length of the document.
Then come exactly T bytes: the document. It may contain line feeds and tabs, which are part of
the document. After these T bytes, the input ends with one more line feed, which is not part of
the document. There are no carriage returns anywhere in the input.
Output
Print k + 1 lines. The first line contains the total number of occurrences of all rules in the
document (rules with equal patterns are each counted). Then, for each rule from 1 to k, print the
number of occurrences of that rule in the document on its own line.
Notes
In the first example the rulebook has 4 rules: ab, b, aba and cat (the comment after the
third rule is ignored, and so is the comment line with a " in it). In the document abababa,
ab occurs 3 times, b 3 times and aba 3 times (overlapping), while cat does not occur, for a
total of 9.
In the second example the rules are, in order: a (written as "\x61"), a again, the
three-byte string #!# (the # characters are inside the literal), the two bytes line feed + tab,
the two characters "\ (a quote and a backslash), the single byte 255, and the string A b with
a space in the middle. The document is the 13 bytes a"\a, line feed, tab, #!#A b#. Both a
rules occur twice, and the byte 255 can never occur in a document.