Threading the Mailbox
Bob exported a mailing-list archive, but only the message headers survived. He wants to know how
the messages group into conversations. Mail clients do this with three header fields:
Message-ID names a message, while In-Reply-To and References name the messages it answers.
All three hold identifiers written in angle brackets, like <[email protected]>.
Two messages belong to the same thread if they share an identifier: it does not matter in which
of the three fields either message mentions it, and the identifier does not have to be the
Message-ID of any message in the archive (the original may simply be missing). Threads are
transitive: if messages A and B share an identifier and B and C share another one, then A, B and C
are all in one thread. A message that mentions no identifiers is a thread on its own.
Print how many threads there are and how large they are.
Archive format
The input is the archive itself: a sequence of lines, each terminated by a line feed (there are no carriage returns). Every line contains only printable ASCII characters (codes 32 to 126) and tabs, and is at most 998 characters long, not counting the line feed. Below, whitespace means spaces and tabs. A line is blank if it is empty; no line consists of whitespace only.
- Messages. The non-blank lines are grouped into messages: each maximal run of consecutive non-blank lines is one message. Any number of blank lines (including none at all before the first message or after the last one, and more than one between two messages) may separate them.
- Fields. A line of a message that starts with whitespace is a continuation line; any other line is a field line. The first line of every message is a field line. A field consists of a field line together with all continuation lines that immediately follow it.
- Field lines. A field line starts with the field name: 1 to 40 characters, each a letter
(
A–Z,a–z), a digit or a hyphen-. The name is immediately followed by a colon:. Everything after that first colon, together with the full text of the field's continuation lines, is the field's value. - Names are case-insensitive:
Message-ID,message-idandMESSAGE-Idare the same field. Only the three namesMessage-ID,In-Reply-ToandReferencesmatter; a name is one of them only if it is equal to it as a whole (for exampleX-Message-IDandReferences2are other fields). A message may contain any of the three fields any number of times, or not at all. - Values of other fields are ignored completely. They may contain any characters, including
<,>and:. - Values of the three fields consist of identifiers and filler text, in any order and amount
(a value may be empty). An identifier is
<, then 1 or more characters with codes 33 to 126 other than<and>, then>. Filler text is any text without<and>; it is ignored. In particular, identifiers may follow each other directly, as in<a@x><b@x>, and an identifier never spans two lines. - Identifiers are compared exactly, character by character:
<ab@x>and<AB@x>are different identifiers.
Input
The archive, as described above. It contains between 1 and 10^5 messages and at most 5·10^6 characters in total.
Output
On the first line print t, the number of threads. On the second line print the t thread sizes
(numbers of messages) in non-increasing order, separated by single spaces.
Notes
In the first example, the first two messages share <[email protected]>. The next three messages
all refer to <[email protected]>, which is not in the archive, so they form one thread of
three; the fourth message's References field continues on the next line. The last message is
alone. The field names are written in several letter cases.
In the second example the archive starts with a blank line, and two blank lines separate the first
two messages. The first message mentions <B1@x> only in its Subject and X-Message-ID fields,
which do not count, so its only identifier is <A1@x>. The second message has the identifiers
<B1@x> and <a1@x> (the text your message of Tue is filler); its line <A1@x> continues the
To field and is ignored. The third message has no identifiers. The fourth message has the
identifiers <c@x>, <d@x>, <a1@x> and <zz@x>, so it joins the second one; <a1@x> differs
from <A1@x>, so the first message stays alone.