Under construction

Still being built. Things may change

CSS Selector Matching

Time limit10sMemory limit256 MB

Summary
Given a nested div document and up to five CSS selectors with descendant and child combinators, print the matching element ids in document order.
Level

Medium5 of 10

Topics
Tree, DFS, Simulation
Solved
No attempts yet

Problem

Modern HTML documents are hierarchical. A document is made of elements, each with a name, attributes, and optional nested elements. Assume only div elements appear.

An element starts with a line of the form:

<div id='name' class='class1 class2 ... classK'>

Its content may be empty or contain zero or more nested elements, then ends with:

</div>

Rules:

  • The element id uses lowercase English letters and is unique in the document.
  • class names also use lowercase letters. One element may belong to many classes, and different elements may share classes.
  • On the opening line, spaces appear only before id, before class, and between adjacent class names.

If element E appears anywhere between F's opening and closing lines, F contains E.

If F contains E and no other element G both contains F and is contained in E, then E is the parent of F.

CSS describes how elements are displayed. This problem uses classifiers and selectors only.

A classifier starts with . and lists dot-separated class names, for example .banana.apple. An element matches a classifier when it has every listed class.

A selector is one or more classifiers separated by spaces, >, or space strings. Matching is defined recursively:

  • A selector that is a single classifier k matches every element with those classes.
  • For T k, element E matches when E matches k and is contained in some element that matches T (not necessarily a direct child).
  • For T > k, element E matches when E matches k and its parent matches T.

Given an N-line document and M selectors, print the ids of all matching elements for each selector in document order.

Input

The first line contains an even integer N (2 ≤ N ≤ 10 000), the number of document lines.

The next N lines are the document. Each line is either an opening or closing tag.

The following line contains M (1 ≤ M ≤ 5), the number of selectors.

The next M lines contain the selectors.

Every id and class uses only lowercase English letters and has length at most 20.

Each input line has length at most 5 000, and the sum of all line lengths is at most 5 000 000.

Output

For each selector, print one line: the match count, then the matching ids in document order separated by spaces. If there are no matches, print 0 only.

Examples1

  1. Example 1

    Input
    22
    <div id='a' class='banana mango'>
    <div id='b' class='orange'>
    <div id='c' class='banana'>
    <div id='d' class='apple mango orange'>
    <div id='e' class='orange'>
    </div>
    </div>
    </div>
    </div>
    <div id='f' class='orange apple'>
    <div id='g' class='apple'>
    <div id='h' class='orange apple'>
    <div id='i' class='banana'>
    </div>
    </div>
    </div>
    <div id='j' class='banana'>
    </div>
    </div>
    </div>
    <div id='k' class='glupo voce daj mi sarme'>
    </div>
    5
    .banana
    .banana > .sarme
    .banana > .orange > .banana
    .banana .apple.orange > .orange
    .mango > .orange .banana
    
    Expected output
    4 a c i j
    0
    2 c j
    1 e
    3 c i j