Reading
Interesting fact about the human brain is that, when reading, it analyses the first and last letter of each word,taking all other not necessarily in the right order to construct the meaning. As a conesqunece, a senetnce whit amlost no corerct word can be raed easliy. ☺
Elly has noticed that shuffling certain letters gives even better result! For example the characters “l” and “i”, “a” and “o” are much similar, than, let’s say, “x” and “m”. She defines a scale from 1 to 5 of difference between letters, where similar ones have low value, and very different have high value. Equal letters have a value of one. In this manner each word can be given a value – the sum of all differences between adjacent letters.
Imagine she has defined the differences between “e” and “l” as 3, between “l” and “y” as 2, between “i” and “l” as 1. Then the word “elly” has value of 3 + 1 + 2 = 6 (remember, that equal letters have distance of 1). The word “lily” has value of 4 and “i” have value of zero =). Longer words are not necessarily with larger value than shorted – consider “lilii” (which is the Bulgarian plural of “lily”) – it has value of only 4, but “elle” (which means “she” in French) has a value of 7. However, each additional letter adds at least one to the value of the word.
Elleonora wants to construct a language that would be easily readable even with huge number of misplaced letters. She has decided to include all non-empty words with values less than or equal to N. Can you help her by finding how many are they?
Notes:
In tests worth 50 points N will not be greater than 1,000,000.
Each pair of letters in the input will be given at most once.
20 10
e l 3
e o 1
o n 2
o r 4
r a 4
i n 5
e n 2
n t 3
t w 3
w i 5Output:
470059518Trivia:some of the words are: ”elleonora”,”entwine”,”aaaaaaaaaaaaaaaaaaaaa”.
Submit solution
Coming laterThe grading service will be connected in a later migration step. You can inspect the task and your previous results now.