Three Bit Computer
Time limit1sMemory limit32 MB
Given a string over {a,b,c}, decide whether an all-uninitialized memory can be initialized to exactly that string with the two given operations.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
The scientists of the Kingdom of Byteland are building a new machine called the Three Bit Computer (TBC). Before it can run, they must solve a problem about how its memory is initialized, and they have asked for your help.
The current TBC has memory cells, numbered from to . Every cell is either uninitialized or holds one of the three values , , or . The machine supports exactly two initialization operations:
- Take two consecutive cells and (with ) that are both uninitialized, and set them to two different values.
- Take two consecutive cells where one is uninitialized and the other already holds a value , and set both of them to the two values different from (that is, the two values of , in either order).
For example, with the following initialization is possible (writing for an uninitialized cell):
Given a target pattern that every cell must end up holding, write a program that decides whether the memory can be initialized to that exact pattern, starting from a completely uninitialized memory.
Input
The first line contains a single integer (), the number of target patterns.
Each pattern is described on two lines:
- the first line contains an integer (), the number of memory cells for that pattern;
- the second line contains a string of length consisting only of the letters , , and — the target pattern itself.
Output
Print lines, one per pattern in the given order. For the -th pattern print YES if the memory can be initialized to that pattern, or NO otherwise.