#P6837. Counting Type A Mushrooms

    ID: 20044 Type: Default 1000ms 256MiB

Counting Type A Mushrooms

Counting Type A Mushrooms

Andrew, a renowned mushroom researcher, has collected (n) mushrooms from Singapore, labeled from (0) to (n-1). Each mushroom belongs to one of two species: (A) or (B). It is known that mushroom (0) is of type (A), but the types of mushrooms (1) through (n-1) are unknown.

To help with his research, Andrew can use a special machine. To use the machine, he selects at least two distinct mushrooms (by their indices) and arranges them in any order in a row. The machine then returns the number of adjacent pairs that are of different types. For example, if the mushrooms arranged have types ( [A, B, B, A] ), the machine returns (2) (since (A) and (B) differ and (B) and (A) differ, while the adjacent pair (B, B) does not contribute).

However, since the machine is expensive to operate, its usage is limited to at most (2\times10^4) calls, and across all calls the total number of mushrooms processed cannot exceed (10^5).

Your task is to help Andrew determine how many mushrooms of type (A) he collected. You must implement a function: [ int\ count_mushrooms(int\ n), ] which will be called exactly once. In your implementation you may call the helper function [ int\ use_machine(int[]\ x), ] where (x) is an array of distinct mushroom indices (with (2 \leq |x| \leq n)) representing the order in which they are placed in the machine. The function (use_machine(x)) returns the number of indices (j) (with (0 \leq j \leq |x|-2)) such that the mushrooms at positions (x[j]) and (x[j+1]) are of different types.

Note: In all your calls to (use_machine), the total number of indices passed (summed over all calls) must not exceed (10^5), and you may call (use_machine) at most (2\times10^4) times.

Input (for simulation purposes): Although in the intended interactive environment you would determine the answer using calls to (use_machine), for simulation your program will read from standard input. The first line contains an integer (n) (the total number of mushrooms). The second line contains a string of length (n) consisting only of the characters 'A' and 'B'. It is guaranteed that the first character of this string is 'A'.

Output: Output a single integer, the number of mushrooms of type (A).

inputFormat

The input consists of two lines. The first line is an integer (n) ((1 \leq n \leq 5\times10^4)), the number of mushrooms. The second line is a string of length (n) with characters 'A' and 'B', where the first character is always 'A'.

outputFormat

Output a single integer: the count of mushrooms of type (A).

sample

1
A
1