Rupsa and the game problem

can anyone explain code of this problem how prefix and suffix stuff they are doing plzz?