Content Filter Rulebook

hard parsing strings string-matching trees bfs 2000 ms · 256 MB

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:

  1. any number (possibly zero) of blanks;
  2. optionally, one literal, followed by any number of blanks;
  3. 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);
  • \n stands for a line feed (code 10);
  • \t stands for a tab (code 9);
  • \xHH, where HH is 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.

Example 1

Input

5
# filter rules, version "1"
"ab"
  "b"
"aba"   # overlapping
"cat"
7
abababa

Output

9
3
3
3
0

Example 2

Input

9
"\x61"
	"a"   # dup

"#!#"#no blank before this comment

"\n\t"
"\"\\"  # a quote, then a backslash
"\xFf"
"A b"
13
a"\a
	#!#A b#

Output

8
2
2
1
1
1
0
1

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

Hint 1

The problem has two parts: reading each literal exactly as the escape rules say, and counting many patterns in one long document. For parsing, why is it unsafe to cut a line at the first # or at the last quote? Read each literal with a single left-to-right scan.

Hint 2

Counting each pattern separately against the document is too slow. Put all the patterns into a trie and run the document through it in one pass. At each position, which patterns end there, including shorter patterns that are suffixes of what has been read so far?

Hint 3

Use Aho-Corasick: failure links send each position to the longest pattern-prefix that is a suffix of the text read so far. Count how often each trie state is visited, then add these counts up the failure tree in reverse BFS order to get each pattern's total. Store transitions sparsely, since a full 256-wide table is too large.

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

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