#P8243. [COCI 2013/2014 #3] REČENICE

[COCI 2013/2014 #3] REČENICE

Problem Description

A long time ago, in a distant country where only math students lived, Iva and Vedran were discussing a special kind of sentence. In these sentences, exactly one word represents a number, and that number equals the total number of letters in the sentence. For example, This sentence has thirtyone letters. and Blah Blah seventeen.

Jurica accidentally overheard his friends’ conversation and decided to impress them with the many special sentences he had memorized. He hurried home and wrote a program that, given a sentence, finds the smallest number that can be inserted to make the sentence special.

Unfortunately, his computer broke, and now he needs your help. Write a program to help Jurica!

Here are some additional notes:

  • The sentence has the form: $\textit{word}_1~\textit{word}_2~\textit{word}_3~\cdots~\textit{word}_{n-1}~\textit{word}_n$. Among these nn words, exactly one is $, which marks the place where the number word should be filled in. For example, this sentence has thirtyone letters would become this sentence has $ letters.
  • The words used to represent numbers follow these rules:
    • For all integers from 11 to 1919, the words representing them in increasing order are one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, and nineteen.
    • For all multiples of ten from 2020 to 9090, the words representing them in increasing order are twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety.
    • For all multiples of one hundred from 100100 to 900900, the words representing them in increasing order are onehundred, twohundred, threehundred, fourhundred, fivehundred, sixhundred, sevenhundred, eighthundred, ninehundred.
    • For all other two-digit numbers, first put the word for the largest multiple of ten not exceeding the number, then concatenate the word for the units digit. For example, the word for the integer 6868 is sixtyeight.
    • For all other three-digit numbers, first put the word for the largest multiple of one hundred not exceeding the number, then concatenate the word for the last two digits (following the previous rules). For example, the words for the integers 319,530,971319,530,971 are threehundrednineteen, fivehundredthirty, and ninehundredseventyone.

Input Format

The first line contains an integer nn, the number of words in the sentence.
Then follow nn lines, each containing a string, representing all the words of the sentence. It is guaranteed that $ appears exactly once among them.

Output Format

Output one line with nn strings, representing the sentence after replacing $ with the word for the smallest number that makes the sentence special.

5
this
sentence
has
$
letters
this sentence has thirtyone letters
7
$
is
the
number
of
letters
here
thirty is the number of letters here
5
the
letters
are
$
potato
the letters are twentynine potato

Hint

[Explanation of Sample 2]

Fill $ with the word thirty, which represents the integer 3030. Then the total number of letters in all words is 6+2+3+6+2+7+4=306+2+3+6+2+7+4=30.

[Explanation of Sample 3]

Note that the sentence in this problem may not satisfy English grammar rules.

[Constraints]

This problem uses bundled testdata. The score and special limits of each subtask are as follows:

  • Subtask 1 (40 pts): the number represented by the word filled into $ does not exceed 5050.
  • Subtask 2 (60 pts): no special limits.

For all testdata, 1n201\leqslant n\leqslant 20, and the number represented by the word filled into $ is less than 10001000.

[Source]

This problem is from COCI 2013-2014 CONTEST 3 T3 REČENICE, using the original data configuration, with a full score of 100100 points.

Translated and organized by Eason_AC.

Translated by ChatGPT 5