Help optimize D solution to phone encoding problem: extremely slow performace.

Siarhei Siamashka siarhei.siamashka at gmail.com
Tue Jan 16 16:56:04 UTC 2024


On Tuesday, 16 January 2024 at 15:50:35 UTC, H. S. Teoh wrote:
> Unfortunately there seems to be some discrepancy between the 
> output I got and the prescribed output in your repository. For 
> example, in your output the number 1556/0 does not have an 
> encoding, but isn't "1 Mai 0" a valid encoding according to 
> your dictionary and the original problem description?

You are not allowed to emit "1" as the first token in the output 
as long as there are any dictionary word matches at that 
position. The relevant paragraph from the problem statement:

==snip==
Encodings of phone numbers can consist of a single word or of 
multiple
words separated by spaces. The encodings are built word by word 
from
left to right. If and only if at a particular point no word at 
all from
the dictionary can be inserted, a single digit from the phone 
number can
be copied to the encoding instead. Two subsequent digits are never
allowed, though. To put it differently: In a partial encoding that
currently covers k digits, digit k+1 is encoded by itself if and 
only if,
first, digit k was not encoded by a digit and, second, there is 
no word
in the dictionary that can be used in the encoding starting at 
digit k+1.
==snip==

I also spent a bit of time trying to figure out this nuance when 
implementing my solution. It doesn't make much sense visually (no 
back-to-back digits in the output either way), but that's how it 
is.


More information about the Digitalmars-d-learn mailing list