Back
Close
  • 354

Learning Opportunities

This puzzle can be solved using the following concepts. Practice using these concepts and improve your skills.

Statement

 Goal

In this challenge, a data format that is a simplified version of XML is used. Tags are identified by a lowercase letter. A start tag is represented by that single letter, and the closing tag is represented by the - character, followed by that letter.

For example, the string "ab-bcd-d-c-ae-e" is the equivalent of <a> <b> </ b> <c> <d> </ d> </ c> </a> <e> </ e> in XML. The supplied string will always be properly formed.

Now we define the depth of a tag as 1 + the number of tags in which it is nested.

In the previous example:
a and e have a depth of 1,
b and c have a depth of 2
and d has a depth of 3.


The weight of a tag name is defined as the sum of the reciprocals of the depths of each of its occurrences.

For example, in the chain a-abab-b-a-b, there are:
- Two tags a with depths of 1 and 2
- Two tags b with depths of 1 and 3.

thus the weight of a is (1/1) + (1/2) = 1.5 and the weight of b is (1/1) + (1/3) = 1.33.

In this challenge you must determine the letter of the tag with the greatest weight in the string argument.
Input
On a single line, a properly formed string of at most 1024 characters representing nested tags.
Output
The letter corresponding to greatest weight tag name. If two tag names have the same weight, display the smallest in alphabetical order.
Constraints
Example
Input
aa-aa-a-a
Output
a

A higher resolution is required to access the IDE