#P8243. Special Sentence Number Word
Special Sentence Number Word
Special Sentence Number Word
In a distant land inhabited only by mathematics students, Iva and Vedran were discussing a special kind of sentence. In these sentences there is exactly one word that stands for a number, and this number must equal the total count of letters in the sentence (ignoring spaces and punctuation). For example, the sentences "This sentence has thirtyone letters." and "Blah Blah seventeen" are special.
Jurica, wanting to impress his friends with a bunch of memorized special sentences, hurried home and wrote a program that, given a sentence, finds the smallest number that can be inserted into the sentence to make it special. Unfortunately, his computer is broken, and he now needs your help. Write a program to help Jurica!
The sentence has the following form:
[ \textit{word}1; \textit{word}2; \cdots ; \textit{word}{i-1}; $ ; \textit{word}{i+1}; \cdots ; \textit{word}_n ]
Here, among the n words, there is exactly one occurrence of the symbol $
, which indicates the position where the number word should be inserted.
The number words are defined as follows:
- For all integers from \(1\) to \(19\), the words are, in increasing order:
one
,two
,three
,four
,five
,six
,seven
,eight
,nine
,ten
,eleven
,twelve
,thirteen
,fourteen
,fifteen
,sixteen
,seventeen
,eighteen
andnineteen
. - For all multiples of ten from \(20\) to \(90\), the words are, in increasing order:
twenty
,thirty
,forty
,fifty
,sixty
,seventy
,eighty
andninety
. - For all multiples of one hundred from \(100\) to \(900\), the words are, in increasing order:
onehundred
,twohundred
,threehundred
,fourhundred
,fivehundred
,sixhundred
,sevenhundred
,eighthundred
andninehundred
. - For other two-digit numbers, concatenate the word for the largest multiple of ten that does not exceed the number with the word for the units digit. For example, \(68\) is represented as
sixtyeight
. - For three-digit numbers that are not exact hundreds, first write the word corresponding to the largest hundred (as above), then append the word for the remaining two-digit number (according to the preceding rules). For example, \(319\) is represented as
threehundrednineteen
.
Your task is to compute the smallest integer \(n\) such that when the $
is replaced by the number word for \(n\), the total number of letters in the sentence equals \(n\). Letters from all words (including the inserted number word) are counted; all other characters (like spaces) are ignored.
inputFormat
The input consists of a single line containing a sentence made up of words separated by spaces. The sentence contains exactly one occurrence of the symbol $
, which is to be replaced by a number word.
Example:
this sentence has $ letters
outputFormat
Output the smallest integer \(n\) that makes the sentence special, according to the rules described.
For the example above, the correct output is 31
because replacing $
with thirtyone
(which has 9 letters) results in a total of 22 (from the other words) + 9 = 31 letters.
sample
this sentence has $ letters
31