Twilight Sparkle's Magic Spells
Input: Standard Input (stdin)
Output: Standard Output (stdout)
Memory limit: 100 megabytes
Time limit: 1.0 seconds
Output: Standard Output (stdout)
Memory limit: 100 megabytes
Time limit: 1.0 seconds
Twilight Sparkle is researching some magic spells. A spell is a non-empty finite sequence of Apples, Bananas, or Ciwifruit.
Spells can be safe or unsafe. An unsafe spell contains one or more of the following patterns:
| Pattern | Name |
|---|---|
AAAAA |
Appleboom |
ABBA |
Stockholm Syndrome |
ABCABC |
Double Rainbow |
Help Twilight determine whether a spell is safe or unsafe.
Input
The first line of input will contain a single integer, 0 \lt N \le 100, representing the length of the spell.
The next line will contain a string of N letters, each either A, B, or C. This is the spell itself.
Output
Output SAFE if the spell is safe; otherwise UNSAFE.
Explanations
-
BAAAAABis unsafe, as it contains an Appleboom. -
AAAACAis safe. While it has five Apples, the Ciwifruit in between means that it can't form an Appleboom. -
ABBABBAis unsafe, as it contains two (overlapping) Stockholm Syndromes. -
ABBAAAAAis also unsafe, as it contains both a Stockholm and an Appleboom. They don't cancel out! -
CBACBAis safe. It has the same fruit as a Double Rainbow, but the fruit are in the wrong order.
-
Sample Input 1
7 BAAAAAB
Sample Output 1
UNSAFE -
Sample Input 2
6 AAAACA
Sample Output 2
SAFE