Repeated Subexpressions in an S-Expression
Alice maintains a large build configuration written in a Lisp-like notation. Over the years people have copied and pasted pieces of it everywhere, and she would like to know how much of it could be factored out into shared definitions.
The configuration is a text made of atoms, lists, whitespace and comments:
document := item*
item := space | comment | atom | "(" item* ")"
- An atom is a maximal run of the characters
A–Z,a–z,0–9and_ + - * / < > = ! ?. Atoms are case-sensitive:Fooandfooare different atoms. Since a run is maximal, two atoms that follow each other are always separated by at least one other character (whitespace, a parenthesis or a comment). - A list is an opening parenthesis
(, the atoms and lists inside it (its elements, in order, possibly none), and the matching closing parenthesis).()is the empty list. - A space is one of the characters space, tab (ASCII 9), carriage return (ASCII 13) and newline (ASCII 10). Any amount of whitespace may appear anywhere outside atoms, including none at all:
(a(b c))is fine. - A comment starts with
;and runs up to (not including) the next newline, or to the end of the text. A comment may contain any printable ASCII character or tab, including(,)and;, and these mean nothing inside it.
Atoms and lists may also appear at the top level, outside any list.
The canonical form of an atom is the atom itself. The canonical form of a list is (, followed by the canonical forms of its elements separated by single spaces, followed by ). For example, both
( f x ;the argument
(g) )
and (f x(g)) have the canonical form (f x (g)), while (a b), (ab) and (a (b)) are three different canonical forms. Two lists are the same if their canonical forms are equal.
Consider every list occurrence in the document: each pair of matching parentheses outside comments is one occurrence, whether it is at the top level or nested inside another list. A list is repeated if at least two occurrences have its canonical form. The size of a list is the total number of atoms inside it at any depth, counted with repetition (the size of (a (b a) ()) is 3).
Count the distinct repeated lists (distinct canonical forms that occur at least twice), and find the largest size among them.
Input
The whole input is the document. It has between 1 and 10^6 characters, and every character is one of: the atom characters listed above, (, ), ;, the four whitespace characters, or (inside comments only) any other printable ASCII character (codes 32 to 126).
The parentheses outside comments are balanced: every ( has a matching ) after it and every ) has a matching ( before it. Nesting is at most 10^5 levels deep: no character lies inside more than 10^5 pairs of matching parentheses.
Output
Print two integers separated by a space: the number of distinct repeated lists, and the largest size of a repeated list. If no list is repeated, print 0 0.
Notes
In the first example, (* x x) occurs twice (once on its own inside the definition of square, once nested inside (* x (* x x))) and has size 3; (square 3) occurs twice and has size 2. Every other list occurs once, so the answer is 2 3.
In the second example, (b c) is written twice with different whitespace and a comment in between, and the empty list () occurs four times. The answer is 2 2: two repeated lists, the larger of which, (b c), has 2 atoms. Note that a repeated empty list has size 0.
The text can be nested 10^5 levels deep, which is too deep for the default call stack in some languages: parse iteratively, or make sure your recursion has enough stack.