Car Park

Interview

Time limit1sMemory limit128 MB

Summary
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 SS and CC (10≤S≤50010 \le S \le 500, 0≤C≤S0 \le C \le S). SS is the total number of spaces in the car park, and CC is the number of cars currently parked there.

The second line of each scenario is a string of up to 255255 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.

  • I means 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).
  • O means 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.

Examples1

  1. Example 1

    Input
    50 12
    IIOIOOIOIIIOOIOIOOII
    25 3
    IOOIOOOOIIO
    0 0
    
    Expected output
    14
    error