Training Site
Sideways banner

Twilight Sparkle's Magic Spells

Input: Standard Input (stdin)
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.


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 SAFE if the spell is safe; otherwise UNSAFE.


  • BAAAAAB is unsafe, as it contains an Appleboom.
  • AAAACA is safe. While it has five Apples, the Ciwifruit in between means that it can't form an Appleboom.
  • ABBABBA is unsafe, as it contains two (overlapping) Stockholm Syndromes.
  • ABBAAAAA is also unsafe, as it contains both a Stockholm and an Appleboom. They don't cancel out!
  • CBACBA is safe. It has the same fruit as a Double Rainbow, but the fruit are in the wrong order.
  • Sample Input 1


    Sample Output 1

  • Sample Input 2


    Sample Output 2