Editorial for COI '13 #1 CSS
Submitting an official solution before solving the problem yourself is a bannable offence.
Given HTML document is comprised of a tree-like structure where the nodes are div elements. Div element is a parent of div element if is directly contained in . Each node in the tree is associated with classes which belong to a corresponding div element.
We can build such a structure in our program simply by using stack and parsing carefully.
A selector can now be viewed as a series of instructions for traversing the tree. For instance, the classifier .banana_.apple.orange > .orange
corresponds to the following series of instructions:
- beginning: pick any node in the tree as a starting point for the search
.banana
: check if the current node's class isbanana
, if not, end search_
: continue searching on any descendant of the current node.apple
: check if the current node's class is.apple
.orange
: check if the current node's class is.orange
>
: continue searching on a direct child of the current node.orange
: check if the current node's class is.orange
- end: end search, this node corresponds to the selector
Such a series of instructions is easy to implement with careful parsing.
Now the tree traversal can be implemented as a recursive function which has two parameters, node and pos. The parameter node describes the current position in the tree, and the parameter pos describes the current position in the series of instructions for traversing the tree.
The recursion's transitions are now performed as described above. Therefore, for >
we move onto a direct descendant, and for .class
we check whether this class exists in the current node.
The transition for _
is a bit more complicated. If we implemented it naively, by iterating over the whole list of descendants, it would be too slow. This is why the recursion will have an additional parameter jump that tells us whether we're in the process of moving onto a child node.
It is necessary to memoize the states of the recursion to ensure that we visit one state only once. The complexity is . The number of states is equal to which is approximately , where is the maximum line length.
Complexity:
Comments