CAGES  Lights, Snakes and Cages
In the zoo there are N cages in which we keep N snakes: each snake in its cage. For experimental reasons, in a month we will move each snake to a different cage: this reordering is given in the input.
Each cage is illuminated by special light, which may be of type A, type B or type C. Your task is to assign the lights A, B, C to the cages so that:
1) each snake will change its lighting, i.e. it will be moved to the cage illuminated differently from the cage it currently inhabits;
2) lightings are equally distributed, i.e. the number of cages A, the number of cages B and the number of cages C differ by at most one.
Input
The first line contains an integer N (2 ≤ N ≤ 300 000), the number of snakes and cages. Cages are numbered from 1 to N.
Each of the next N lines contains an integer. When Kth of these lines contains the number L, it means that the snake from the cage K will be moved to the cage L (K ≠ L). Two snakes will not move to the same cage.
Output
Print the string composed of the characters A, B and C, such that the Kth character denotes the lighting of the cage K.
If there are multiple solutions, print any.
Example
Input: 4
4
3
2
1
Output: ACBC
hide comments
aeon:
20160917 23:00:58
@psetter Kindly have a look at my solution ( id : 17732677 ), I have considered even the cyclic cases, but still I am getting WA after 15 test cases. 

avisheksanvas:
20160706 11:15:11
@psetter please check my submission : 17234107!


codeforsure:
20160522 08:42:18
@author can you please check my submission: 16964085, getting wrong answer after 15 test cases.


rajatgupta:
20160422 23:12:54
Cant find test cases... getting WA...HELP!!!! 

lt:
20160322 17:42:43
5 WAs :( , I'm to figure out what's wrong. Can somebody help me? 

Sergio Vieri:
20150614 06:17:02
Solved with 341 lines in Assembly.. yay 

kancha:
20141219 11:09:00
well, tried debugging with random case generator, no success :(


Adrian Satja Kurdija:
20140803 10:11:23
@Bhavik: have you written a random case generator and a checker? I'm sure you'll find your bug that way. 

Bhavik:
20140616 18:06:13
@psetter:can you look my id:11776229 and kindly tell if i my approach is right?? Last edit: 20140617 17:23:28 

Adrian Satja Kurdija:
20140328 08:18:14
Many WAs in submissions :) I suggest using random case generator and a checker to find your bugs. 
Added by:  Adrian Satja Kurdija 
Date:  20140324 
Time limit:  1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ASM64 
Resource:  Croatia nationals 2014 (6th grade, 3rd problem "Zmije") 