Mixing Coins
Time limit5sMemory limit512 MB
Groups of equal coins are merged three at a time when three consecutive same-material coins appear, and the survivor count is requested.
- Level
Medium7 of 10
- Topics
- Simulation, Stack, Implementation
- Solved
- No attempts yet
Problem
Misaka fires coins from a railgun. To fight crime she lays out a line of coins. A stronger coin comes from mixing coins together, and coins of different materials do not combine, so she only mixes coins of the same material.
She makes a coin like this.
- Scanning from the front of the line, find the first position where three consecutive coins share a material.
- Take those three coins out of the line.
- Mix them into one new coin of the same material.
- Put the new coin at the back of the line.
Misaka repeats these steps until she cannot make another coin.
Count the coins left in the line when she stops.
Input
The first line contains a single integer , the number of test cases.
The first line of each test case contains an integer , the number of groups of consecutive coins. All coins lie in a single line.
Each of the next lines contains a character and an integer , meaning that the -th group is consecutive coins of material and sits directly behind the -th group.
- is an uppercase letter, and for every with
Output
For each test case, print on one line the number of coins left when no new coin can be made.