problems / 286

Repeated Subexpressions in an S-Expression

medium parsing rolling-hash hashing stack strings 1500 ms · 256 MB

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–9 and _ + - * / < > = ! ?. Atoms are case-sensitive: Foo and foo are 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.

Example 1

Input

; build helpers
(define (square x) (* x x))
(define (cube x) (* x (* x x)))
(print (square 3) (cube 3))
(print (square 3))

Output

2 3

Example 2

Input

(a(b c)() ; a comment with (b c) in it
  (   b
	c ) ())
(()
 ())

Output

2 2

Hints · reveal one at a time if you're stuck

Hint 1

If you could turn every list into one short key, so that equal canonical forms give equal keys, the rest is counting keys in a map. Building each canonical string explicitly is too slow, though: a list nested 10^5 levels deep is copied into every list around it.

Hint 2

A list's canonical form is built from its elements' canonical forms. Can the key of a list be computed from the keys (and lengths) of its elements alone, at the moment its ) is read? Keep a stack of the lists that are still open.

Hint 3

Use a polynomial hash: hash(X + Y) = hash(X) · B^len(Y) + hash(Y), so a list's hash comes from its elements' hashes and lengths in O(number of elements). Use two moduli: there can be hundreds of thousands of lists. See rolling hash.

report an issue Prefer your own editor? sooks prep 286 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.