In this problem we will use the terms XHTML and CSS. They do not match the real-world XHTML and CSS standards exactly. Read the problem statement to clarify the meanings of these terms in the problem context.
You are given a String[] xhtml. Concatenate elements of xhtml to get a single string formatted as tags. We use BNF to describe tags formally.
-
tags ::= tag | tag tags
-
tagContent ::= EMPTY | tags
-
tag ::= <TAG id='ID' style='color:COLOR'>tagContent</TAG>
Where
-
EMPTY means an empty string,
-
TAG is one of the strings "b", "u", "i", which are called tag names.
-
ID means unique non-empty tag identifier containing only lowercase letters,
-
COLOR is one of the following seven standard colors: "black", "blue", "gray", "green",
"red", "white", "yellow".
The tags in tagContent are called strict descendants of the surrounding tag.
We will say that each tag is assigned the specified color. For example, if tags = "<b id='x' style='color:white'><u id='y' style='color:red'></u><u id='z' style='color:red'></u></b>", then the tag with id='x' is assigned the color white, and the tags with id='y' and id='z' are assigned the color red.
You decided to extract the information about assigned colors to CSS rules. Each CSS rule looks like "selector {color:COLOR;}" and assigns a specific color to one or more tags. In this problem we will accept only two types of selectors:
-
"#id" ? means that CSS rule will be applied to the tag with the given id (example, "#x").
-
"#id tagName" ? means that CSS rule will be applied to all tags with the specified tag name which are the strict descendants of the tag with the given id (example, "#x u").
CSS rules are applied in the order they are given, and it is possible for some later rules to override earlier rules for certain tags.
Return the minimal number of CSS rules you need to assign the proper color to each tag.
|