\[ \let\oldlor\lor \renewcommand{\lor}{\; \oldlor \;} \let\oldland\land \renewcommand{\land}{\; \oldland \;} \renewcommand{\set}[1]{\{\, #1 \,\}} \renewcommand{\given}{\,\mid\,} \renewcommand{\abs}[1]{\lvert #1 \rvert} \renewcommand{\divs}{\!\;\mid\;\!} \renewcommand{\ndivs}{\!\;\nmid\;\!} \renewcommand{\betw}[3][1]{#1 \leq #2 \leq #3} \renewcommand{\mod}[1]{\ (\mathrm{mod}\ #1)} \renewcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \renewcommand{\ceil}[1]{\left \lceil #1 \right \rceil} \renewcommand{\t}[1]{\texttt{#1}} \renewcommand{\fori}[2][i]{\text{for } #1 = 0, 1, \dots, #2} \renewcommand{\x}[1]{\text{#1}} \renewcommand\concat{\mathbin{+\mkern-10mu+}} \DeclareMathOperator*{\CONCAT}{\concat} \DeclareMathOperator*{\SCC}{\|} \]
UVa 230 - Borrowers
Time limit: 3.0 seconds
UVa Difficulty rating: 3
View on UVa

This is an easier problem that only requires one dimensional array manipulation and careful input parsing. If you haven't already, carefully read the problem description before proceeding. First and foremost, we need to parse the sample input for the title, author pairs until we reach the first occurrence of END. The format of each non-END line will be as follows: title" by author Note the separator string is literally " by. We don't want to include the last " character as part of the title, as it may affect the sort order. My approach was to use getline as the conditional expression in a while-loop to read the input up to the first newline character, breaking when the extracted contents matched string literal END. For each line containing a title-author pair, I use string find to search the string for the given string separator, split the string using string substr, and store the pair.

Note we are storing the pair in author-title order to get the benefits of built-in sorting mechanism of pair within a set. Although I chose to insert each pair into a set, you could also use a vector of pairs, and call sort on it upon breaking the while-loop. However, you may not use a map to store the pairs in author-title order. Take a moment to consider why this approach cannot work. To keep it simple, I chose to store the information associated with title, author, and state (i.e. on-shelf, off-shelf, pending-return) each in its own one-dimensional array, ordered appropriately.

Next, we need to read line by line and perform operations based on line format. We stop processing more lines when we reach the END line. There are three formats:

  • BORROW title
  • RETURN title
  • SHELVE

We can parse this in a number of ways. Since your choice of how to process input will rarely, if ever, result in TLE you are free to use whatever method is fastest to code or easiest to visually verify correctness. If you ever do a getline after reading with cin >> ..., you must issue a cin.ignore() to remove the newline character from the input stream. cin will leave trailing whitespace in the stream, so if you don't clear it before a getline you could end up with an empty string you didn't expect. Moving on... I read the next word with cin, breaking if the contents match END. If the contents match BORROW or RETURN, I read the next word (remembering to consume the trailing whitespace left from cin), trim it appropriately, and use the find function template, included with the <algorithm> header, to get an iterator to the matching element in my title vector. This is an iterator of type vector<string>::iterator, but we can convert it to a 0-based index by subtracting the begin() container iterator from it.

Once we hit the SHELVE command, we iterate over all elements of the st vector, and for each element where st[i] == 2, we need to output one line indicating what book the ith book should be placed after (with a special case when it is the first book in the shelf order).

My print() function, used in the SHELVE block above:

That's it! These problems are great practice for beginners since they get practice using getline and cin together. It is far better to make mistakes in practice and correct them, than to be confused in a contest environment over a language construct.

Links

C++ References

UVa

230 Borrowers
543 Goldbach's Conjecture
900 Brick Wall Patterns
10047 The Monocycle
10140 Prime Distance
10165 Stone Game
10338 Mischievous Children
10394 Twin Primes
10892 LCM Cardinality

CodeForces

1A Theatre Square
4A Watermelon
25A IQ test
50A Domino piling
58A Chat room
59A Word
69A Young Physicist
71A Way Too Long Words
96A Football
112A Petya and Strings
118A String Task
131A cAPS lOCK
158A Next Round
189A Cut Ribbon
230B T-primes
231A Team
263A Beautiful Matrix
281A Word Capitalization
282A Bit++
318A Even Odds
339A Helpful Maths
339B Xenia and Ringroad
455A Boredom
459B Pashmak and Flowers
474B Worms
479A Expression
486A Calculating Function
489B BerSU Ball
489C Given Length and Sum of Digits...
492B Vanya and Lanterns
500A New Year Transportation
507B Amr and Pins
513A Game
520B Two Buttons
550A Two Substrings
580A Kefa and First Steps
742A Arpa's hard exam and Mehrdad's naive cheat
766B Mahmoud and a Triangle
935A Fafa and his Company
977B Two-gram
977D Divide by three, multiply by two
996A Hit the Lottery
1097A Gennady and a Card Game
1108A Two distinct points
1154A Restoring Three Numbers