Car Park
InterviewTime limit1sMemory limit128 MB
Simulate a sequence of car entry and exit signals for a car park of capacity S starting with C cars, stopping early if an exit occurs while empty.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, String
- Solved
- No attempts yet
Problem
Please build a management system for a car park. The car park has a fixed number of parking spaces. It wants to tell approaching drivers how many spaces are currently available, and it must keep the entry barrier from opening once the park is full.
For each car park (scenario) you are given the number of cars already parked and a string of signals from the entry and exit barriers. Process the signals in order, and report how many cars remain in the car park at the end of each scenario.
Input
The input consists of several scenarios, each representing a different car park.
The first line of each scenario contains two integers and (, ). is the total number of spaces in the car park, and is the number of cars currently parked there.
The second line of each scenario is a string of up to characters, each of which is the upper-case letter I or O. This string represents a stream of data from the entry and exit barriers.
Imeans that a car has attempted to enter the car park. If the car park is not full, the car is allowed in and counted. If every space is occupied, the car is refused entry and is not counted (it may try again later).Omeans that a car has driven out of the car park.
If there are no cars in the car park when an O appears in the stream, that O is an error, and processing of that scenario stops immediately.
The last scenario is 0 0; this scenario is not processed.
Output
Print one line for each scenario. If there was an error in the data stream, the line contains just the word error. Otherwise, print a single integer: the number of cars in the car park at the end of the scenario.