#P11138. APC String Reduction

    ID: 13199 Type: Default 1000ms 256MiB

APC String Reduction

APC String Reduction

You are given a string \(S\) consisting only of the characters A, P, and C. You may repeatedly remove a subsequence \(A P C\) (i.e. there exist indices \(i, j, k\) with \(i < j < k\) and \(S[i]=A,\ S[j]=P,\ S[k]=C\)) from \(S\). Continue removing such subsequences as long as possible.

After no further removals can be made, output the final string. If the string becomes empty, output Perfect.

The mathematical formulation of the removal step is as follows: if there exist indices \(i, j, k\) such that \[ \exists\, i,j,k:\; i < j < k,\quad S[i]=A,\quad S[j]=P,\quad S[k]=C, \] remove the characters at those positions. The process is repeated until no such triple exists.

inputFormat

The input consists of a single line containing the string \(S\). It is guaranteed that \(S\) only contains the characters A, P, and C.

outputFormat

Output the string after performing the maximal removals. If the resulting string is empty, output Perfect.

sample

APC
Perfect