Bard Prodigy
Time limit1sMemory limit512 MB
Given a poem and per-key press limits, decide whether the poem plus its derived title (first letter of each word, uppercased) can be typed, counting runs of equal keys as one press, and print the title or -1.
- Level
Easy3 of 10
- Topics
- String, Implementation, Simulation
- Solved
- No attempts yet
Problem
Yeongjae, a bard with a sensitive soul, often gets sudden flashes of poetic inspiration in the middle of everyday life.
But Yeongjae has a poor memory, so the moment inspiration strikes, he has to write it down on the computer or he loses it. A poem consists of uppercase and lowercase letters and spaces. His inspirations are always excellent, but Yeongjae lacks a sense of titles, so he takes the first letter of each word in the poem, changes it to uppercase, and concatenates them in order to form the title. If the poem is 'There is no cow level', the title is 'TINCL'.
Yeongjae's keyboard, worn out from recording poems at all hours, is near the end of its life. The number of times he can press the space bar and the letter keys is fixed; exceeding it kills the keyboard and no further work is possible. One fortunate thing: when the same character appears consecutively or spaces appear consecutively while typing, Yeongjae can hold the key down and use it only once, making the keyboard a bit more efficient. (A and a are different characters, so 'Aa' counts as pressing the a key twice.)
The poem and the title are separated by the Enter key. Fortunately, the Shift key and the Enter key always have unlimited life.
Given the poem Yeongjae wrote this time and the number of times the space bar and letter keys can be pressed, print the title if the poem and the title can both be recorded, and print -1 if the keyboard dies and the recording cannot be completed.
Input
The first line gives the poem.
The second line gives the remaining number of times the space bar can be used.
The third line gives the remaining number of times each of the 26 letter keys can be used, in alphabetical order, with case ignored.
Output
If the poem and the title can both be recorded, print the title.
If the keyboard dies and the recording cannot be completed, print -1.
Constraints
- The poem is a string of nonzero length, not exceeding 5,000 characters, consisting of lowercase letters, uppercase letters, and spaces.
- The poem begins and ends with a letter, not a space.
- The space bar is not used except when typing the poem.
- The remaining number of times the space bar and letter keys can be used is a nonnegative integer not exceeding 10,000.