problems / 140

Threading the Mailbox

medium union-find parsing strings hashing 1000 ms · 256 MB

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-id and MESSAGE-Id are the same field. Only the three names Message-ID, In-Reply-To and References matter; a name is one of them only if it is equal to it as a whole (for example X-Message-ID and References2 are 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.

Example 1

Input

Message-ID: <[email protected]>
Subject: Weekly sync

message-id: <[email protected]>
In-Reply-To: <[email protected]>
Subject: Re: Weekly sync

Message-Id: <[email protected]>
References: <[email protected]>

MESSAGE-ID: <[email protected]>
References: <[email protected]>
 <[email protected]>

in-reply-to: <[email protected]>

Message-ID: <[email protected]>

Output

3
3 2 1

Example 2

Input

Message-ID: <A1@x>
Subject: see <B1@x> and <a1@x>
X-Message-ID: <B1@x>


Message-ID: <B1@x>
In-Reply-To: your message of Tue <a1@x>
To: Bob <bob@x>
 <A1@x>

Subject: no ids here
From: carol

Message-ID: <c@x><d@x>
References:
	<a1@x>,<zz@x>

Output

3
2 1 1

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

Hint 1

Treat every identifier as a node joining the messages that mention it, and ask how many groups form when messages sharing identifiers are merged. A missing parent identifier still connects its replies.

Hint 2

Parse each message by its fields: only the values of the three named fields count, and continuation lines belong to the field above them, so decide by the first character of each line. Compare field names as whole words in lowercase, but keep identifiers case-sensitive.

Hint 3

Use a union-find structure over messages, with a map from each identifier to the first message that mentioned it. At the end, count the component sizes and sort them in descending order.

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

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